School Commit Init
This commit is contained in:
+79
@@ -0,0 +1,79 @@
|
||||
*
|
||||
!*/
|
||||
!*.*
|
||||
# Ignore compiled binaries
|
||||
*.exe
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
*.rar
|
||||
.vs/
|
||||
# Ignore build output
|
||||
build/
|
||||
dist/
|
||||
target/
|
||||
bin/
|
||||
obj/
|
||||
x64/
|
||||
x86/
|
||||
lib/
|
||||
|
||||
# Ignore IDE and editor files
|
||||
.idea/
|
||||
.vscode/
|
||||
*.sublime-project
|
||||
*.sublime-workspace
|
||||
|
||||
# Ignore system files
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
# Ignore logs and temporary files
|
||||
*.log
|
||||
*.tmp
|
||||
|
||||
# Ignore package manager directories
|
||||
node_modules/
|
||||
vendor/
|
||||
|
||||
# Ignore sensitive or environment-specific information
|
||||
.env
|
||||
secrets/
|
||||
|
||||
# Ignore user-specific files
|
||||
*.bak
|
||||
*.swp
|
||||
*.swo
|
||||
|
||||
# Ignore generated documentation
|
||||
docs/
|
||||
|
||||
# Ignore database files
|
||||
*.db
|
||||
*.sqlite
|
||||
|
||||
# Ignore compiled code
|
||||
*.class
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
|
||||
# Ignore OS-specific files
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
# Ignore backup files
|
||||
*~
|
||||
*.bak
|
||||
__pycache__/
|
||||
|
||||
*.obj
|
||||
*.lst
|
||||
*.ilk
|
||||
*.pdb
|
||||
*.bin
|
||||
*.out
|
||||
*.gcov
|
||||
*.gcno
|
||||
*.gcda
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
;An unsigned number a on 32 bits is given. Print the hexadecimal
|
||||
;representation of a, but also the results of the circular permutations
|
||||
;of its hex digits.
|
||||
|
||||
bits 32
|
||||
global start
|
||||
|
||||
extern exit,printf
|
||||
import exit msvcrt.dll
|
||||
import printf msvcrt.dll
|
||||
|
||||
%include "subprogram.asm"
|
||||
|
||||
segment data use32 class=data
|
||||
format db "%X ",0
|
||||
a dd 12ABCDEFh
|
||||
permutations resd 8
|
||||
|
||||
segment code use32 class=code
|
||||
start:
|
||||
push dword [a]
|
||||
push dword permutations
|
||||
call subprogram
|
||||
add ESP,4*2
|
||||
mov ECX,8
|
||||
label:
|
||||
push ECX
|
||||
push dword [permutations+ECX*8]
|
||||
push dword format
|
||||
call [printf]
|
||||
add ESP,4*2
|
||||
pop ECX
|
||||
loop label
|
||||
push dword 0
|
||||
call [exit]
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
%ifndef _SUBPROGRAM_ASM_
|
||||
%define _SUBPROGRAM_ASM_
|
||||
subprogram:
|
||||
mov ECX,8
|
||||
mov EBX,[ESP+4]
|
||||
mov EAX,[ESP+8]
|
||||
.label:
|
||||
mov [EBX+ECX*8],EAX
|
||||
rol EAX,4
|
||||
loop .label
|
||||
ret
|
||||
|
||||
%endif
|
||||
@@ -0,0 +1,43 @@
|
||||
;Read a string of unsigned numbers in base 10 from keyboard.
|
||||
;Determine the maximum value of the string and write it in the file
|
||||
;max.txt (it will be created) in 16 base.
|
||||
|
||||
bits 32
|
||||
|
||||
global start
|
||||
|
||||
extern exit,printf,scanf
|
||||
import exit msvcrt.dll
|
||||
import printf msvcrt.dll
|
||||
import scanf msvcrt.dll
|
||||
|
||||
%include "subprogram.asm"
|
||||
%include "writefile.asm"
|
||||
|
||||
segment data use32 class=data
|
||||
dd 0
|
||||
read_format db "%100[0-9a-zA-Z ]",0
|
||||
write_format db "%X",0
|
||||
text_file db "text_file.txt",0
|
||||
mode db "w",0
|
||||
string times 101 db 0
|
||||
max resd 1
|
||||
|
||||
segment code use32 class=code
|
||||
start:
|
||||
push dword string
|
||||
push dword read_format
|
||||
call [scanf]
|
||||
add ESP,4*2
|
||||
push dword max
|
||||
push dword string
|
||||
call get_max
|
||||
add ESP,4*2
|
||||
push dword write_format
|
||||
push EAX
|
||||
push dword mode
|
||||
push dword text_file
|
||||
call write_to_file
|
||||
add ESP, 4*2
|
||||
push dword 0
|
||||
call [exit]
|
||||
@@ -0,0 +1,38 @@
|
||||
%ifndef _SUBPROGRAM_ASM_
|
||||
%define _SUBPROGRAM_ASM_
|
||||
|
||||
table db ""
|
||||
maximum dd 0
|
||||
|
||||
get_max:
|
||||
mov ESI,[ESP+4]
|
||||
begin:
|
||||
mov ECX,0
|
||||
loop_label:
|
||||
lodsb
|
||||
cmp AL,' '
|
||||
je eon
|
||||
cmp AL,0
|
||||
je zero
|
||||
sub AL,"0"
|
||||
mov EBX,0
|
||||
mov BL,AL
|
||||
mov EAX,ECX
|
||||
mov EDX,10
|
||||
mul EDX
|
||||
add EAX,EBX
|
||||
mov ECX,EAX
|
||||
jmp loop_label
|
||||
eon:
|
||||
cmp ECX,[maximum]
|
||||
jb begin
|
||||
mov [maximum],ECX
|
||||
jmp begin
|
||||
zero:
|
||||
cmp ECX,[maximum]
|
||||
jb ending
|
||||
mov [maximum],ECX
|
||||
ending:
|
||||
mov EAX,[maximum]
|
||||
ret
|
||||
%endif
|
||||
@@ -0,0 +1 @@
|
||||
41
|
||||
@@ -0,0 +1,28 @@
|
||||
%ifndef _WRITEFILE_ASM_
|
||||
%define _WRITEFILE_ASM_
|
||||
|
||||
extern fopen,fprintf,fclose
|
||||
import fopen msvcrt.dll
|
||||
import fprintf msvcrt.dll
|
||||
import fclose msvcrt.dll
|
||||
|
||||
write_to_file:
|
||||
mov EAX,[ESP+4*1]
|
||||
mov EBX,[ESP+4*2]
|
||||
push EBX
|
||||
push EAX
|
||||
call [fopen]
|
||||
add ESP,4*2
|
||||
mov ECX,[ESP+4*3]
|
||||
mov EDX,[ESP+4*4]
|
||||
push ECX
|
||||
push EDX
|
||||
push EAX
|
||||
call [fprintf]
|
||||
pop EAX
|
||||
add ESP,4*2
|
||||
push EAX
|
||||
call [fclose]
|
||||
add ESP,4*1
|
||||
ret
|
||||
%endif
|
||||
@@ -0,0 +1,11 @@
|
||||
#include<stdio.h>
|
||||
void subprogram(int* p,int a);
|
||||
int main(){
|
||||
int a=0x12345678;
|
||||
int p[8];
|
||||
subprogram(p,a);
|
||||
for (int i=7;i>=0;i--){
|
||||
printf("%x ",p[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
bits 32
|
||||
global _subprogram
|
||||
|
||||
segment code use32 class=code public
|
||||
_subprogram:
|
||||
mov ECX,8
|
||||
mov EBX,[ESP+4]
|
||||
mov EAX,[ESP+8]
|
||||
.label:
|
||||
mov [EBX+ECX*4-4],EAX
|
||||
rol EAX,4
|
||||
loop .label
|
||||
ret
|
||||
@@ -0,0 +1,13 @@
|
||||
#include<stdio.h>
|
||||
|
||||
void subprogram(char a[], int b[]);
|
||||
|
||||
int main(){
|
||||
char a[]="10100111b01100011b110b101011b";
|
||||
int b[]={0,0,0,0,0,0,0,0,0,0};
|
||||
subprogram(a,b);
|
||||
for (int i=0;b[i]!=0;i++){
|
||||
printf("%d ",b[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
bits 32
|
||||
global _subprogram
|
||||
|
||||
segment code use32 class=code public
|
||||
_subprogram:
|
||||
cld
|
||||
mov ESI,[ESP+4]
|
||||
mov EDI,[ESP+8]
|
||||
mov EAX,0
|
||||
mov EBX,0
|
||||
label:
|
||||
lodsb
|
||||
cmp byte AL,0
|
||||
je end
|
||||
cmp byte AL,'b'
|
||||
jne skip
|
||||
mov EAX,EBX
|
||||
stosd
|
||||
mov EBX,0
|
||||
jmp label
|
||||
skip:
|
||||
sub AL,'0'
|
||||
mov ECX,0
|
||||
mov Cl,AL
|
||||
mov EAX,EBX
|
||||
mov EDX,2
|
||||
mul EDX
|
||||
mov EBX,EAX
|
||||
add EBX,ECX
|
||||
jmp label
|
||||
end:
|
||||
ret
|
||||
@@ -0,0 +1,32 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
a db 1
|
||||
b db 2
|
||||
c db 5
|
||||
d db 1
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
mov al,[a]
|
||||
add al, [d]
|
||||
mov bl, [b]
|
||||
add bl, [d]
|
||||
sub [c],al
|
||||
add bl,[c]
|
||||
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,31 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
a db 10
|
||||
b db 4
|
||||
c db 3
|
||||
d db 12
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
mov al,[a]
|
||||
add al,13
|
||||
sub al,[c]
|
||||
add al,[d]
|
||||
sub al,7
|
||||
add al,[b]
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,31 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
a dw 10
|
||||
b dw 13
|
||||
c dw 42
|
||||
d dw 16
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
mov ax,[c]
|
||||
add ax,[b]
|
||||
add ax,[a]
|
||||
mov bx,[d]
|
||||
add bx,[d]
|
||||
sub ax,bx
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,30 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
a dw 10
|
||||
b dw 31
|
||||
c dw 13
|
||||
d dw 10
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
mov ax,[a]
|
||||
add ax,[b]
|
||||
add ax,[b]
|
||||
mov bx,[c]
|
||||
sub bx,[d]
|
||||
add ax,bx
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
a db 10
|
||||
b db 12
|
||||
c db 8
|
||||
d dw 45
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
mov al,[a]
|
||||
add al,[b]
|
||||
sub al,[c]
|
||||
mov bl,2
|
||||
mul bl
|
||||
add ax,[d]
|
||||
sub ax,5
|
||||
mov bx,[d]
|
||||
mul bx
|
||||
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
a db 10
|
||||
b db 12
|
||||
c db 5
|
||||
d dw 45
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
mov ax,0
|
||||
mov al,[a]
|
||||
add al,[b]
|
||||
mov bl,2
|
||||
div bl
|
||||
mov bl,al
|
||||
mov ax,0
|
||||
mov al,[a]
|
||||
div byte [c]
|
||||
mov cl,10
|
||||
sub cl,al
|
||||
add bl,cl
|
||||
mov ax,0
|
||||
mov al,[b]
|
||||
mov cl,4
|
||||
div cl
|
||||
add bl,al
|
||||
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
a db 10
|
||||
b db 2
|
||||
c db 16
|
||||
d db 42
|
||||
e dw 52
|
||||
f dw 16
|
||||
g dw 36
|
||||
h dw 28
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
mov al,[a]
|
||||
sub al,[b]
|
||||
mov bl,4
|
||||
mul bl
|
||||
div byte [c]
|
||||
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
a db 10
|
||||
b db 25
|
||||
c db 2
|
||||
d db 64
|
||||
e dw 25
|
||||
f dw 17
|
||||
g dw 85
|
||||
h dw 19
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
mov al,[a]
|
||||
mul al
|
||||
mov bx,[e]
|
||||
add bx,[f]
|
||||
sub ax,bx
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,24 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
mov al,1
|
||||
add al,9
|
||||
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,25 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
mov al,4
|
||||
mov bl,4
|
||||
mul bl
|
||||
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
a db 12h
|
||||
b dw 74A1h
|
||||
c dd 243F2149h
|
||||
d dq 768AC79B8296E852h
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
mov AL,[a] ;AL=a
|
||||
cbw
|
||||
cwde ;EAX=a
|
||||
mov EBX,EAX ;EBX=a
|
||||
mov AX,[b] ;AX=b
|
||||
cwde ;EAX=b
|
||||
add EAX,[c] ;EAX=c+b
|
||||
add EAX,EBX ;EAX=c+b+a
|
||||
cdq ;EDX:EAX=c+b+a
|
||||
mov EBX,[d]
|
||||
mov ECX,[d+4] ;ECX:EBX=d
|
||||
add EBX,[d]
|
||||
adc ECX,[d+4] ;ECX:EBX=d+d
|
||||
sub EAX,EBX
|
||||
sbb EDX,ECX ;EDX:EAX=(c+b+a)-(d+d)
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
a db 12h
|
||||
b dw 74A1h
|
||||
c dd 243F2149h
|
||||
d dq 768AC79B8296E852h
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
mov AL,[a] ;AL=a
|
||||
cbw
|
||||
cwde
|
||||
cdq ;EDX:EAX=a
|
||||
mov EBX,[d]
|
||||
mov ECX,[d+4] ;ECX:EBX=d
|
||||
sub EBX,EAX
|
||||
sbb ECX,EDX ;ECX:EBX=d-a
|
||||
sub EAX,[c] ;EAX=a-c
|
||||
cdq
|
||||
sub EBX,EAX
|
||||
sbb ECX,EDX ;ECX:EBX=(d-a)-(a-c)
|
||||
sub EBX,[d]
|
||||
sbb ECX,[d+4] ;ECX:EBX=(d-a)-(a-c)-d
|
||||
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
a db 12h
|
||||
b dw 74A1h
|
||||
c dd 243F2149h
|
||||
d dq 768AC79B8296E852h
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
mov EBX,0 ;EBX=0
|
||||
mov BL,[a] ;EBX=a
|
||||
mov EDX,[d+4] ;
|
||||
mov EAX,[d] ;EDX:EAX=d
|
||||
add EAX,EBX
|
||||
adc EDX,0 ;EDX:EAX=a+d
|
||||
mov EBX,[c]
|
||||
mov ECX,0 ;ECX:EBX=c
|
||||
sub EBX,EAX
|
||||
sbb ECX,EDX ;ECX:EBX=c-(a+d)
|
||||
push EBX
|
||||
push ECX
|
||||
mov EDX,[d+4]
|
||||
mov EAX,[d] ;EDX:EAX=d
|
||||
mov ECX,0
|
||||
mov CX,[b] ;ECX=b
|
||||
add EAX,ECX
|
||||
adc EDX,0 ;EDX:EAX=b+d
|
||||
pop ECX
|
||||
pop EBX ;ECX:EBX=c-(a+d)
|
||||
add EAX,EBX
|
||||
adc EDX,ECX ;EDX:EAX=c-(a+d)+(b+d)
|
||||
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
a db 12h
|
||||
b dw 74A1h
|
||||
c dd 243F2149h
|
||||
d dq 768AC79B8296E852h
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
mov EAX,0
|
||||
mov EBX,0
|
||||
mov AL,[a] ;EAX=a
|
||||
mov BX,[b] ;EBX=b
|
||||
add EBX,EAX ;EBX=b+a
|
||||
mov ECX,[c] ;ECX=c
|
||||
sub ECX,EAX ;ECX=c-a
|
||||
sub ECX,EBX ;ECX=c-a-(b+a)
|
||||
add ECX,[c] ;ECX=c-a-(b+a)+c
|
||||
|
||||
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,39 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
a db 12h
|
||||
b dd 7483AB72h
|
||||
c dq 768AC79B8296E852h
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
mov AL,[a] ;AL=a
|
||||
imul byte [a] ;AX=a*a
|
||||
cwde ;EAX=a*a
|
||||
sub EAX,[b] ;EAX=a*a-b
|
||||
add EAX,7 ;EAX=a*a-b+7
|
||||
push EAX
|
||||
mov AL,[a] ;AL=a
|
||||
cbw
|
||||
cwde ;EAX=a
|
||||
add EAX,2 ;EAX=2+a
|
||||
mov EBX,EAX ;EBX=2+a
|
||||
pop EAX ;EAX=a*a-b+7
|
||||
cdq ;EDX:EAX=a*a-b+7
|
||||
idiv EBX ;EAX=(a*a-b+7)/(2+a)
|
||||
cdq ;EDX:EAX=(a*a-b+7)/(2+a)
|
||||
add EAX,[c]
|
||||
adc EDX,[c+4] ;EDX:EAX=c+(a*a-b+7)/(2+a)
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,58 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
a dw 7293h
|
||||
b db 0A8h
|
||||
c dw 0B749h
|
||||
d db 4Bh
|
||||
e dd 7483AB72h
|
||||
x dq 768AC79Bh
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
mov EAX,[x]
|
||||
mov EDX,[x+4] ;EDX:EAX=x
|
||||
mov EBX,2 ;EBX=2
|
||||
idiv EBX ;EAX=x/2
|
||||
push EAX
|
||||
mov AL,[b] ;AL=b
|
||||
cbw ;AX=b
|
||||
mov BX,AX ;BX=b
|
||||
pop EAX
|
||||
add BX,[a] ;BX=a+b
|
||||
mov ECX,EAX ;ECX=x/2
|
||||
mov AX,100 ;AX=100
|
||||
imul BX ;EAX=100*(a+b)
|
||||
add ECX,EAX ;ECX=x/2+100*(a+b)
|
||||
mov AX,3 ;AX=3
|
||||
cwd ;DX:AX=3
|
||||
push AX
|
||||
mov AL,[d] ;AL=d
|
||||
cbw ;AX=d
|
||||
mov BX,AX ;BX=d
|
||||
pop AX
|
||||
add BX,[c] ;BX=c+d
|
||||
idiv BX ;AX=3/(c+d)
|
||||
cwde
|
||||
sub ECX,EAX ;ECX=x/2+100*(a+b)-3/(c+d)
|
||||
mov EAX,ECX
|
||||
cdq
|
||||
mov EBX,EDX
|
||||
mov EAX,[e] ;EAX=e
|
||||
imul dword [e] ;EDX:EAX=e*e
|
||||
add EAX,ECX
|
||||
adc EDX,EBX ;EDX:EAX=x/2+100*(a+b)-3/(c+d)+e*e
|
||||
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,37 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
a db 12h
|
||||
b dd 7483AB72h
|
||||
c dq 768AC79B8296E852h
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
mov AL,[a] ;AL=a
|
||||
mul byte [a] ;AX=a*a
|
||||
mov BX,AX
|
||||
mov EAX,0
|
||||
mov AX,BX ;EAX=a*a
|
||||
sub EAX,[b] ;EAX=a*a-b
|
||||
add EAX,7 ;EAX=a*a-b+7
|
||||
mov EDX,0 ;EDX:EAX=a*a-b+7
|
||||
mov EBX,0 ;EBX=0
|
||||
mov BL,[a] ;EBX=a
|
||||
add EBX,2 ;EBX=2+a
|
||||
div EBX ;EAX=(a*a-b+7)/(2+a)
|
||||
mov EDX,0 ;EDX:EAX=(a*a-b+7)/(2+a)
|
||||
add EAX,[c]
|
||||
adc EDX,[c+4] ;EDX:EAX=c+(a*a-b+7)/(2+a)
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,50 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
a dw 7293h
|
||||
b db 0A8h
|
||||
c dw 0B749h
|
||||
d db 4Bh
|
||||
e dd 7483AB72h
|
||||
x dq 768AC79Bh
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
mov EAX,[x]
|
||||
mov EDX,[x+4] ;EDX:EAX=x
|
||||
mov EBX,2 ;EBX=2
|
||||
div EBX ;EAX=x/2
|
||||
mov BX,0
|
||||
mov BL,[b] ;BX=b
|
||||
add BX,[a] ;BX=a+b
|
||||
mov ECX,EAX ;ECX=x/2
|
||||
mov AX,100 ;AX=100
|
||||
mul BX ;EAX=100*(a+b)
|
||||
add ECX,EAX ;ECX=x/2+100*(a+b)
|
||||
mov AX,3 ;AX=3
|
||||
mov DX,0 ;DX:AX=3
|
||||
mov BX,0 ;BX=0
|
||||
mov BL,[d] ;BX=d
|
||||
add BX,[c] ;BX=c+d
|
||||
div BX ;AX=3/(c+d)
|
||||
mov EBX,0
|
||||
mov BX,AX ;EBX=3/(c+d)
|
||||
sub ECX,EBX ;ECX=x/2+100*(a+b)-3/(c+d)
|
||||
mov EAX,[e] ;EAX=e
|
||||
mul dword [e] ;EDX:EAX=e*e
|
||||
add EAX,ECX
|
||||
adc EDX,0 ;EDX:EAX=x/2+100*(a+b)-3/(c+d)+e*e
|
||||
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,46 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
a db 4Fh
|
||||
b dw 8241h
|
||||
c resd 1
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
mov ECX,0
|
||||
mov CL,1111_1111b
|
||||
mov EAX,0
|
||||
mov AL,[a]
|
||||
and AL,1111_0000b
|
||||
shl EAX,4
|
||||
or ECX,EAX
|
||||
|
||||
mov EBX,0
|
||||
mov BX,[b]
|
||||
and EBX, 0000_0011_1111_1100b
|
||||
shl EBX,10
|
||||
or ECX,EBX
|
||||
mov EAX,0
|
||||
mov AL,[a]
|
||||
and AL,0000_1111b
|
||||
shl EAX,20
|
||||
or ECX,EAX
|
||||
mov EAX,0
|
||||
mov AL,[b+1]
|
||||
ror EAX,8
|
||||
or ECX,EAX
|
||||
mov [c],ECX
|
||||
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,42 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
a dw 4A2Fh
|
||||
b dw 8241h
|
||||
c dw 931Dh
|
||||
d resw 1
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
mov AX,[a]
|
||||
and AX,0000_0000_0011_1110b
|
||||
shr AX,1
|
||||
|
||||
mov BX,[b]
|
||||
and BX,0000_0111_1100_0000b
|
||||
shr BX,6
|
||||
|
||||
mov CX,[c]
|
||||
and CX,1111_1000_0000_0000b
|
||||
shr CX,11
|
||||
|
||||
mov DX,0
|
||||
add DX,AX
|
||||
add DX,BX
|
||||
add DX,CX
|
||||
mov [d],DX
|
||||
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,50 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
|
||||
|
||||
;Given a byte string S of length l, obtain the string D of length l-1 as D(i) = S(i) * S(i+1) (each element of D is the product of two consecutive elements of S).
|
||||
|
||||
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
S db 1,2,3,4
|
||||
l equ $-S
|
||||
D resw l-1
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
; mov ECX,0
|
||||
; jmp_label:
|
||||
; mov AL,[S+ECX]
|
||||
; mov BL,[S+ECX+1]
|
||||
; imul BL
|
||||
; mov [D+2*ECX],AX
|
||||
; inc ECX
|
||||
; cmp ECX,l-1
|
||||
; jb jmp_label
|
||||
|
||||
mov ECX,l
|
||||
mov ESI,0
|
||||
jmp_label:
|
||||
mov AL,[S+ESI]
|
||||
mov BL,[S+ESI+1]
|
||||
imul BL
|
||||
mov [D+2*ESI],AX
|
||||
inc ESI
|
||||
loop jmp_label
|
||||
|
||||
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,46 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
|
||||
;Two character strings S1 and S2 are given. Obtain the string D by concatenating the elements found on odd positions in S2 and the elements found on even positions in S1.
|
||||
|
||||
segment data use32 class=data
|
||||
S1 db 'abcbef'
|
||||
l1 equ $-S1
|
||||
S2 db '123456'
|
||||
l2 equ $-S2
|
||||
D resb l1/2+l2/2+l2 % 2
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
mov EDI,0
|
||||
mov ESI,0
|
||||
S2_label:
|
||||
mov AL,[S2+ESI]
|
||||
mov [D+EDI],AL
|
||||
inc EDI
|
||||
add ESI,2
|
||||
cmp ESI,l2
|
||||
jb S2_label
|
||||
mov ESI,0
|
||||
S1_label:
|
||||
mov AL,[S1+1+ESI*2]
|
||||
mov [D+EDI],AL
|
||||
inc ESI
|
||||
inc EDI
|
||||
cmp ESI,l1/2
|
||||
jb S1_label
|
||||
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,47 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
a dd 127f5678h,0abcdabcdh
|
||||
l equ $-$$
|
||||
b resb l
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
mov ECX,l
|
||||
mov ESI,a
|
||||
mov EDI,b
|
||||
CLD
|
||||
|
||||
label:
|
||||
LODSB
|
||||
cbw
|
||||
mov BX,AX
|
||||
LODSB
|
||||
cbw
|
||||
mov DX,AX
|
||||
LODSB
|
||||
cbw
|
||||
add BX,AX
|
||||
LODSB
|
||||
cbw
|
||||
add AX,DX
|
||||
rol EAX,16
|
||||
mov AX,BX
|
||||
STOSD
|
||||
sub ECX,4
|
||||
jg label
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,59 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
s1 db 12h,78h
|
||||
l1 equ $-s1
|
||||
s2 db 34h,0ABh
|
||||
l2 equ $-s2
|
||||
s3 resb l1+l2
|
||||
l3 equ $-s3
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
mov ECX,l3
|
||||
mov ESI,0
|
||||
mov EDX,0
|
||||
mov EDI,s3
|
||||
cld
|
||||
|
||||
label:
|
||||
mov AL,0FFh
|
||||
cmp ESI,l1
|
||||
jge a
|
||||
mov AL,[s1+ESI]
|
||||
a:
|
||||
mov BL,0FFh
|
||||
cmp EDX,l1
|
||||
jge b
|
||||
mov BL,[s2+EDX]
|
||||
b:
|
||||
cmp AL,BL
|
||||
jb less
|
||||
mov AL,BL
|
||||
inc EDX
|
||||
STOSB
|
||||
dec ECX
|
||||
jmp comp
|
||||
less:
|
||||
inc ESI
|
||||
dec ECX
|
||||
STOSB
|
||||
jmp comp
|
||||
comp:
|
||||
cmp ECX,0
|
||||
jg label
|
||||
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,51 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit,scanf ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
import scanf msvcrt.dll
|
||||
|
||||
;Read two numbers a and b (in base 10) from the keyboard and calculate their product. This value will be stored in a variable called "result" (defined in the data segment).
|
||||
|
||||
|
||||
segment data use32 class=data
|
||||
query db "%d",0
|
||||
a dd 0
|
||||
b dd 0
|
||||
result dq 0
|
||||
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
push dword a
|
||||
push dword query
|
||||
call [scanf]
|
||||
add ESP,4*2
|
||||
|
||||
push dword b
|
||||
push dword query
|
||||
call [scanf]
|
||||
add ESP,4*2
|
||||
|
||||
mov EAX,[a]
|
||||
imul dword [b]
|
||||
mov [result],EAX
|
||||
mov [result+4],EDX
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
|
||||
|
||||
import socket
|
||||
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
|
||||
msg="hey"
|
||||
sleep(10)
|
||||
s.sendto(str.encode(msg),("127.0.0.1",5555))
|
||||
msg,adr=s.recvfrom(10)
|
||||
print (msg.decode())
|
||||
@@ -0,0 +1,47 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit,scanf,printf ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
import scanf msvcrt.dll
|
||||
import printf msvcrt.dll
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
|
||||
;Read two numbers a and b (in base 10) from the keyboard. Calculate and print their arithmetic average in base 16
|
||||
|
||||
segment data use32 class=data
|
||||
read_query db "%d",0
|
||||
write_query db "The arithmetic average in base 16 is: 0x%08X",0
|
||||
a dd 0
|
||||
b dd 0
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
push dword a
|
||||
push dword read_query
|
||||
call [scanf]
|
||||
add ESP,4*2
|
||||
|
||||
push dword b
|
||||
push dword read_query
|
||||
call [scanf]
|
||||
add ESP,4*2
|
||||
|
||||
mov EAX,[a]
|
||||
add EAX,[b]
|
||||
sar EAX,1
|
||||
|
||||
push EAX
|
||||
push write_query
|
||||
call [printf]
|
||||
add ESP, 4*2
|
||||
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,72 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit,fopen,fclose,printf,fread,strchr ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
import fopen msvcrt.dll
|
||||
import fclose msvcrt.dll
|
||||
import printf msvcrt.dll
|
||||
import fread msvcrt.dll
|
||||
import strchr msvcrt.dll
|
||||
|
||||
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
|
||||
|
||||
;A text file is given. Read the content of the file, count the number of vowels and display the result on the screen. The name of text file is defined in the data segment.
|
||||
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
file_path db "text.txt",0
|
||||
access_mode db "r",0
|
||||
vowels db "aeiouAEIOU",0
|
||||
message db "The number of vowels in the file is: %d",0
|
||||
file dd 0
|
||||
a db 0
|
||||
count dd 0
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
push dword access_mode
|
||||
push dword file_path
|
||||
call [fopen]
|
||||
add ESP,4*2
|
||||
cmp EAX,0
|
||||
jz end
|
||||
mov [file],EAX
|
||||
|
||||
push dword [file]
|
||||
push dword 1
|
||||
push dword 1
|
||||
push dword a
|
||||
loop_label:
|
||||
call [fread]
|
||||
cmp EAX,0
|
||||
jz done
|
||||
push dword [a]
|
||||
push dword vowels
|
||||
call [strchr]
|
||||
add ESP,4*2
|
||||
cmp EAX,0
|
||||
jz loop_label
|
||||
inc dword [count]
|
||||
jmp loop_label
|
||||
done:
|
||||
add ESP,4*4
|
||||
; exit(0)
|
||||
push dword [file]
|
||||
call [fclose]
|
||||
add ESP,4*1
|
||||
push dword [count]
|
||||
push dword message
|
||||
call [printf]
|
||||
add ESP,4*2
|
||||
end:
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1 @@
|
||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
|
||||
@@ -0,0 +1,86 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit,fopen,fclose,printf,fread ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
import fopen msvcrt.dll
|
||||
import fclose msvcrt.dll
|
||||
import printf msvcrt.dll
|
||||
import fread msvcrt.dll
|
||||
|
||||
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
|
||||
|
||||
;A text file is given. Read the content of the file, count the number of letters 'y' and 'z' and display the values on the screen. The file name is defined in the data segment.
|
||||
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
file_path db "text.txt",0
|
||||
access_mode db "r",0
|
||||
message db `The number of 'y' in the file is: %d\nThe number of 'z' in the file is: %d`,0
|
||||
file dd 0
|
||||
a db 0
|
||||
count_y dd 0
|
||||
count_z dd 0
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
push dword access_mode
|
||||
push dword file_path
|
||||
call [fopen]
|
||||
add ESP,4*2
|
||||
cmp EAX,0
|
||||
jz end
|
||||
mov [file],EAX
|
||||
|
||||
push dword [file]
|
||||
push dword 1
|
||||
push dword 1
|
||||
push dword a
|
||||
|
||||
loop_label:
|
||||
call [fread]
|
||||
cmp EAX,0
|
||||
jz done
|
||||
|
||||
cmp byte [a],"y"
|
||||
jz inc_y
|
||||
cmp byte [a],"Y"
|
||||
jz inc_y
|
||||
|
||||
cmp byte [a],"z"
|
||||
jz inc_z
|
||||
cmp byte [a],"Z"
|
||||
jz inc_z
|
||||
|
||||
jmp loop_label
|
||||
|
||||
inc_y:
|
||||
inc dword [count_y]
|
||||
jmp loop_label
|
||||
|
||||
inc_z:
|
||||
inc dword [count_z]
|
||||
jmp loop_label
|
||||
|
||||
done:
|
||||
add ESP,4*4
|
||||
push dword [file]
|
||||
call [fclose]
|
||||
add ESP,4*1
|
||||
|
||||
push dword [count_z]
|
||||
push dword [count_y]
|
||||
push dword message
|
||||
call [printf]
|
||||
add ESP,4*3
|
||||
end:
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1 @@
|
||||
But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure? On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee
|
||||
@@ -0,0 +1 @@
|
||||
abcD;aC#
|
||||
@@ -0,0 +1,88 @@
|
||||
;Se citesc dintr-un fisier caractere, pana la intalnirea caracterului
|
||||
;#. Sa se afiseze la consola numarul literelor mici, urmat de numarul
|
||||
;literelor mari citite.
|
||||
|
||||
bits 32
|
||||
global start
|
||||
extern exit,printf,fread,fopen,fclose
|
||||
|
||||
|
||||
import exit msvcrt.dll
|
||||
import printf msvcrt.dll
|
||||
import fread msvcrt.dll
|
||||
import fopen msvcrt.dll
|
||||
import fclose msvcrt.dll
|
||||
|
||||
|
||||
segment data use32 class=data
|
||||
file_path db "file.txt",0
|
||||
mode db "r",0
|
||||
file resd 1
|
||||
char resb 1
|
||||
upcount dd 0
|
||||
locount dd 0
|
||||
upwrite db "The number of upper case letters: %d",10,13,0
|
||||
lowrite db "The number of lower case letters: %d",10,13,0
|
||||
|
||||
|
||||
|
||||
segment code use32 class=code
|
||||
start:
|
||||
push dword mode
|
||||
push dword file_path
|
||||
call [fopen]
|
||||
add esp,4*2
|
||||
mov [file], EAX
|
||||
cmp EAX,0
|
||||
je error
|
||||
|
||||
|
||||
loop_label:
|
||||
push dword [file]
|
||||
push dword 1
|
||||
push dword 1
|
||||
push dword char
|
||||
call [fread]
|
||||
add esp,4*4
|
||||
cmp EAX,"0"
|
||||
je end
|
||||
cmp byte [char],"#"
|
||||
je end
|
||||
cmp byte [char],"A"
|
||||
jl loop_label
|
||||
cmp byte [char],"z"
|
||||
jg loop_label
|
||||
cmp byte [char],"Z"
|
||||
jle upper
|
||||
cmp byte [char],"a"
|
||||
jge lower
|
||||
jmp loop_label
|
||||
|
||||
|
||||
upper:
|
||||
inc dword [upcount]
|
||||
jmp loop_label
|
||||
lower:
|
||||
inc dword [locount]
|
||||
jmp loop_label
|
||||
|
||||
end:
|
||||
push dword [file]
|
||||
call [fclose]
|
||||
add esp, 4*1
|
||||
|
||||
push dword [locount]
|
||||
push dword lowrite
|
||||
call [printf]
|
||||
add esp,4*2
|
||||
|
||||
push dword [upcount]
|
||||
push dword upwrite
|
||||
call [printf]
|
||||
add esp,4*2
|
||||
|
||||
error:
|
||||
|
||||
push dword 0
|
||||
call [exit]
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
a db 2
|
||||
b db 2
|
||||
c db 3
|
||||
d db 4
|
||||
x resb 1
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
mov al,[a]
|
||||
add al,[b]
|
||||
mul byte [c]
|
||||
div byte [d]
|
||||
mov [x],al
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,36 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
a db 10
|
||||
b db 2
|
||||
c db 3
|
||||
d db 4
|
||||
x resb 1
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
mov al,[b]
|
||||
mul byte [c]
|
||||
mov bl,[a]
|
||||
mov bh,0
|
||||
sub bx,ax
|
||||
mov ax,bx
|
||||
div byte [d]
|
||||
mov [x],al
|
||||
|
||||
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,35 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
a db 10
|
||||
b dw 2
|
||||
c db 3
|
||||
d db 4
|
||||
x resw 1
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
mov ah,0
|
||||
mov al,[a]
|
||||
mul word [b]
|
||||
mov bl,[d]
|
||||
mov bh,0
|
||||
div bx
|
||||
mov bl,[c]
|
||||
sub ax,bx
|
||||
mov [x],ax
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,56 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
a db 10
|
||||
b dw 2
|
||||
c dd 3
|
||||
d db 4
|
||||
f dw 1
|
||||
g dw 2
|
||||
x dq 2
|
||||
|
||||
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
mov al,[a] ;AL=a
|
||||
cbw ;AX=a
|
||||
imul word [b] ;DX:AX=a*b
|
||||
push dx ;stack: dx,
|
||||
push ax ;stack: dx,ax
|
||||
mov al,[d] ;AL=d
|
||||
cbw ;AX=d
|
||||
cwde ;EAX=d
|
||||
mov ecx,[c] ;ECX=c
|
||||
sub ecx,eax ;ECX=c-d
|
||||
pop eax ;EAX=a*b , stack:
|
||||
cdq ;EDX:EAX=a*b
|
||||
idiv dword ecx ;EAX=(a*b)/(c-d)
|
||||
mov ecx,eax ;ECX=(a*b)/(c-d)
|
||||
mov ax,[f] ;AX=f
|
||||
imul word [g] ;DX:AX=f*g
|
||||
push dx ;stack: DX
|
||||
push ax ;stack: DX,AX
|
||||
pop eax ;EAX=f*g , stack:
|
||||
add eax,ecx ;EAX=(a*b)/(c-d)+f*g
|
||||
cdq ;EDX:EAX=(a*b)/(c-d)+f*g
|
||||
add [x],eax
|
||||
adc [x+4],edx
|
||||
|
||||
|
||||
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,44 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
a dd 1
|
||||
b db 1
|
||||
c dw 1
|
||||
d dw 1
|
||||
x resd 1
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
mov ax,[a] ;AX=a-low
|
||||
mov dx,[a+2] ;DX=a-high DX:AX=a-low
|
||||
mov bx,2 ;BX=2
|
||||
idiv bx ;AX=a/2
|
||||
mov dx,ax ;DX=a/2
|
||||
mov al,[b] ;AL=b
|
||||
mov bl,3 ;BL=3
|
||||
imul bl ;AX=b*3
|
||||
add dx,ax
|
||||
mov ax,[c]
|
||||
imul word [d]
|
||||
push dx
|
||||
push ax
|
||||
pop eax
|
||||
mov eax,ecx
|
||||
mov ax,dx
|
||||
cwde
|
||||
sub eax,ecx
|
||||
add eax,100
|
||||
mov [x],eax
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,32 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
a db 'message'
|
||||
l equ $-a
|
||||
b resb l
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
mov ESI,l
|
||||
label:
|
||||
mov AL,[a+ESI-1]
|
||||
|
||||
sub AL,'a'-'A'
|
||||
mov [b+ESI-1],AL
|
||||
dec ESI
|
||||
jne label
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,22 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,34 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
a db "coaie"
|
||||
l equ $-$$
|
||||
b resb l
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
mov ESI,a
|
||||
mov EDI,b
|
||||
mov ECX,l
|
||||
cld
|
||||
jecxz end
|
||||
label:
|
||||
lodsb
|
||||
sub al,"a"-"A"
|
||||
stosb
|
||||
loop label
|
||||
end:
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,36 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
a db "coaie"
|
||||
l equ $-$$
|
||||
b resb l
|
||||
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
mov ESI,a
|
||||
mov EDI,a+2*l-1
|
||||
mov ECX,l
|
||||
jecxz end
|
||||
label:
|
||||
CLD
|
||||
lodsb
|
||||
STD
|
||||
stosb
|
||||
loop label
|
||||
end:
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,29 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
s1 dw 1,10,32
|
||||
l1 equ ($-s1)/2
|
||||
s2 dw 0FF23h,0AF21h,0B25h
|
||||
l2 equ ($-s2)/2
|
||||
s resb l1+l2
|
||||
e db 10
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
mov ax,256
|
||||
mov bl,1
|
||||
div bl
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,35 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit,printf,scanf ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
import printf msvcrt.dll ; printf is a function that prints a string to the standard output. It is defined in msvcrt.dll
|
||||
import scanf msvcrt.dll ; scanf is a function that reads a string from the standard input. It is defined in msvcrt.dll
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
message db "n=",0
|
||||
n dd 0
|
||||
read db "%d",0
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
;Write a programs that prints the message "n=" on the screen and then read from keyword the value for the signed number n
|
||||
push dword message
|
||||
call [printf]
|
||||
add ESI,4*1
|
||||
push dword n
|
||||
push dword read
|
||||
call [scanf]
|
||||
add ESI,4*2
|
||||
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,43 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit,printf,scanf ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
import printf msvcrt.dll ; printf is a function that prints a formatted string to the standard output
|
||||
import scanf msvcrt.dll ; scanf is a function that reads a formatted string from the standard input
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
read_number db "%d",0
|
||||
a dd 0
|
||||
b dd 0
|
||||
message db "Sum=%d",0
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
; read a and b
|
||||
push dword a
|
||||
push dword read_number
|
||||
call [scanf]
|
||||
add esp, 4*2
|
||||
push dword b
|
||||
push dword read_number
|
||||
call [scanf]
|
||||
add esp, 4*2
|
||||
;print out the sum
|
||||
mov eax, [a]
|
||||
add eax, [b]
|
||||
push eax
|
||||
push dword message
|
||||
call [printf]
|
||||
add esp, 4*2
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,100 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit,fopen,fprintf,fscanf,fread,fwrite,remove,rename,fclose
|
||||
import exit msvcrt.dll ; exit is a function that terminates the program. It is defined in msvcrt.dll
|
||||
import fopen msvcrt.dll ; fopen is a function that opens a file. It is defined in msvcrt.dll
|
||||
import fprintf msvcrt.dll ; fprintf is a function that prints a formatted string to a file. It is defined in msvcrt.dll
|
||||
import fscanf msvcrt.dll ; fscanf is a function that reads a formatted string from a file. It is defined in msvcrt.dll
|
||||
import fread msvcrt.dll ; fread is a function that reads a block of data from a file. It is defined in msvcrt.dll
|
||||
import fwrite msvcrt.dll ; fwrite is a function that writes a block of data to a file. It is defined in msvcrt.dll
|
||||
import remove msvcrt.dll ; remove is a function that deletes a file. It is defined in msvcrt.dll
|
||||
import rename msvcrt.dll ; rename is a function that renames a file. It is defined in msvcrt.dll
|
||||
import fclose msvcrt.dll ; fclose is a function that closes a file. It is defined in msvcrt.dll
|
||||
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
file_path_read db "a.txt", 0 ; the path to the file we want to open
|
||||
file_path_write db "b.txt", 0 ; the path to the file we want to open
|
||||
access_mode_read db "r", 0 ; the access mode we want to use when opening the file
|
||||
access_mode_write db "w", 0 ; the access mode we want to use when opening the file
|
||||
file_read dd 0 ; the file pointer returned by fopen
|
||||
file_write dd 0 ; the file pointer returned by fopen
|
||||
read_value dd 0 ; the value read from the file
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; ...
|
||||
; open the file
|
||||
push dword access_mode_read ; push the parameter for fopen onto the stack
|
||||
push dword file_path_read ; push the parameter for fopen onto the stack
|
||||
call [fopen] ; call fopen to open the file
|
||||
add esp, 4*2 ; remove the parameters from the stack
|
||||
cmp eax, 0 ; compare the return value of fopen with 0
|
||||
jz exit_label ; if fopen returned 0, then the file could not be opened, so we exit_label the program
|
||||
mov [file_read], eax ; save the file pointer returned by fopen in the variable file
|
||||
|
||||
push dword access_mode_write ; push the parameter for fopen onto the stack
|
||||
push dword file_path_write ; push the parameter for fopen onto the stack
|
||||
call [fopen] ; call fopen to open the file
|
||||
add esp, 4*2 ; remove the parameters from the stack
|
||||
cmp eax, 0 ; compare the return value of fopen with 0
|
||||
jz exit_label ; if fopen returned 0, then the file could not be opened, so we exit_label the program
|
||||
mov [file_write], eax ; save the file pointer returned by fopen in the variable file
|
||||
|
||||
; read the file
|
||||
loop_label:
|
||||
|
||||
push dword [file_read] ; push the parameter for fread onto the stack
|
||||
push dword 1 ; push the parameter for fread onto the stack
|
||||
push dword 1 ; push the parameter for fread onto the stack
|
||||
push dword read_value ; push the parameter for fread onto the stack
|
||||
call [fread] ; call fread to read the file
|
||||
add esp, 4*4 ; remove the parameters from the stack
|
||||
cmp eax, 0 ; compare the return value of fread with 0
|
||||
jz end ; if fread returned 0, then the file could not be read, so we exit_label the program
|
||||
|
||||
inc byte [read_value] ; increment the value read from the file
|
||||
|
||||
push dword [file_write] ; push the parameter for fwrite onto the stack
|
||||
push dword 1 ; push the parameter for fwrite onto the stack
|
||||
push dword 1 ; push the parameter for fwrite onto the stack
|
||||
push dword read_value ; push the parameter for fwrite onto the stack
|
||||
call [fwrite] ; call fwrite to write the file
|
||||
add esp, 4*4 ; remove the parameters from the stack
|
||||
cmp eax, 0 ; compare the return value of fwrite with 0
|
||||
jz exit_label ; if fwrite returned 0, then the file could not be written, so we exit_label the program
|
||||
jmp loop_label
|
||||
|
||||
end:
|
||||
|
||||
; close the file
|
||||
push dword [file_read] ; push the parameter for fclose onto the stack
|
||||
call [fclose] ; call fclose to close the file
|
||||
add esp, 4 ; remove the parameter from the stack
|
||||
push dword [file_write] ; push the parameter for fclose onto the stack
|
||||
call [fclose] ; call fclose to close the file
|
||||
add esp, 4 ; remove the parameter from the stack
|
||||
|
||||
;remove a.txt file
|
||||
push dword file_path_read ; push the parameter for remove onto the stack
|
||||
call [remove] ; call remove to remove the file
|
||||
add esp, 4 ; remove the parameter from the stack
|
||||
|
||||
|
||||
;rename b.txt to a.txt
|
||||
push dword file_path_read ; push the parameter for rename onto the stack
|
||||
push dword file_path_write ; push the parameter for rename onto the stack
|
||||
call [rename] ; call rename to rename the file
|
||||
add esp, 4*2 ; remove the parameters from the stack
|
||||
|
||||
|
||||
exit_label:
|
||||
push dword 0 ; push the parameter for exit_label onto the stack
|
||||
call [exit] ; call exit_label to terminate the program
|
||||
@@ -0,0 +1 @@
|
||||
efg
|
||||
@@ -0,0 +1,26 @@
|
||||
bits 32
|
||||
global start
|
||||
extern exit,function,printf
|
||||
import exit msvcrt.dll
|
||||
import printf msvcrt.dll
|
||||
|
||||
|
||||
segment data use32 class=data
|
||||
str1 db "hello ",0
|
||||
str2 db "world",0
|
||||
new_str resb 100
|
||||
format db "%s",0
|
||||
|
||||
segment code use32 class=code
|
||||
start:
|
||||
push dword str1
|
||||
push dword str2
|
||||
push dword new_str
|
||||
call function
|
||||
add ESP,4*3
|
||||
push dword new_str
|
||||
push dword format
|
||||
call [printf]
|
||||
add ESP,4*2
|
||||
push dword 0
|
||||
call [exit]
|
||||
@@ -0,0 +1,19 @@
|
||||
bits 32
|
||||
global function
|
||||
|
||||
|
||||
segment code use32 class=code public
|
||||
function:
|
||||
mov esi, [esp+12]
|
||||
mov edi, [esp+4]
|
||||
loop1:
|
||||
movsb
|
||||
cmp [esi], byte 0
|
||||
jne loop1
|
||||
mov esi, [esp+8]
|
||||
loop2:
|
||||
movsb
|
||||
cmp [esi], byte 0
|
||||
jne loop2
|
||||
mov [edi], byte 0
|
||||
ret
|
||||
@@ -0,0 +1,30 @@
|
||||
bits 32
|
||||
global start
|
||||
|
||||
extern sum,exit,printf,scanf
|
||||
import exit msvcrt.dll
|
||||
import printf msvcrt.dll
|
||||
import scanf msvcrt.dll
|
||||
|
||||
segment data use32 class=data
|
||||
format db "%u",0
|
||||
n dd 0
|
||||
result dd 0
|
||||
|
||||
segment code use32 class=code
|
||||
start:
|
||||
push dword n
|
||||
push dword format
|
||||
call [scanf]
|
||||
add ESP,4*2
|
||||
push dword result
|
||||
push dword [n]
|
||||
call sum
|
||||
add ESP,4*2
|
||||
push dword [result]
|
||||
push dword format
|
||||
call [printf]
|
||||
add ESP,4*2
|
||||
push dword 0
|
||||
call [exit]
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
bits 32
|
||||
global sum
|
||||
|
||||
segment code use32 class=code
|
||||
sum:
|
||||
mov EAX,[ESP+4]
|
||||
mov EBX,0
|
||||
mov ECX,10
|
||||
loop1:
|
||||
cmp eax,0
|
||||
je end
|
||||
mov EDX,0
|
||||
div ECX
|
||||
add EBX,EDX
|
||||
jmp loop1
|
||||
|
||||
end:
|
||||
mov EDX,[ESP+8]
|
||||
mov [EDX],EBX
|
||||
ret
|
||||
@@ -0,0 +1,70 @@
|
||||
bits 32
|
||||
global start
|
||||
extern exit, printf, scanf, fopen, fclose, fread
|
||||
import exit msvcrt.dll
|
||||
import printf msvcrt.dll
|
||||
import scanf msvcrt.dll
|
||||
import fopen msvcrt.dll
|
||||
import fclose msvcrt.dll
|
||||
import fread msvcrt.dll
|
||||
|
||||
|
||||
segment data use32 class=data
|
||||
format db "%X", 0
|
||||
n dd 0
|
||||
path db "textfile.txt", 0
|
||||
mode db "r", 0
|
||||
filehandle dd 0
|
||||
s db 0
|
||||
string resb 17
|
||||
|
||||
|
||||
segment code use32 class=code
|
||||
start:
|
||||
push dword n
|
||||
push dword format
|
||||
call [scanf]
|
||||
add esp, 4*2
|
||||
|
||||
push dword mode
|
||||
push dword path
|
||||
call [fopen]
|
||||
add esp, 4*2
|
||||
cmp eax, 0
|
||||
jz end
|
||||
mov [filehandle], eax
|
||||
|
||||
mov eax,0
|
||||
mov ax, [n]
|
||||
mov edi, string
|
||||
loop_label:
|
||||
push eax
|
||||
push edi
|
||||
push dword [filehandle]
|
||||
push dword 1
|
||||
push dword 1
|
||||
push dword s
|
||||
call [fread]
|
||||
add esp, 4*3
|
||||
cmp eax, 0
|
||||
jz end
|
||||
pop edi
|
||||
pop eax
|
||||
shr eax, 1
|
||||
jnc loop_label
|
||||
mov edx,[s]
|
||||
mov [edi], edx
|
||||
inc edi
|
||||
jmp loop_label
|
||||
end:
|
||||
mov byte [edi], 0
|
||||
push dword string
|
||||
call [printf]
|
||||
add esp, 4
|
||||
push dword [filehandle]
|
||||
call [fclose]
|
||||
add esp, 4
|
||||
push dword 0
|
||||
call [exit]
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
0123456789abcdef
|
||||
@@ -0,0 +1,25 @@
|
||||
bits 32 ; assembling for the 32 bits architecture
|
||||
|
||||
; declare the EntryPoint (a label defining the very first instruction of the program)
|
||||
global start
|
||||
|
||||
; declare external functions needed by our program
|
||||
extern exit ; tell nasm that exit exists even if we won't be defining it
|
||||
import exit msvcrt.dll ; exit is a function that ends the calling process. It is defined in msvcrt.dll
|
||||
; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
|
||||
|
||||
; our data is declared here (the variables needed by our program)
|
||||
segment data use32 class=data
|
||||
; ...
|
||||
a db 255
|
||||
b dd 100h
|
||||
|
||||
|
||||
; our code starts here
|
||||
segment code use32 class=code
|
||||
start:
|
||||
|
||||
|
||||
; exit(0)
|
||||
push dword 0 ; push the parameter for exit onto the stack
|
||||
call [exit] ; call exit to terminate the program
|
||||
@@ -0,0 +1,23 @@
|
||||
; The code below will calculate the result of some arithmetic operations in the EAX register, save the value of the registers, then display the result value and restore the value of the registers.
|
||||
bits 32
|
||||
|
||||
global start
|
||||
|
||||
; declare extern functions
|
||||
extern exit, printf
|
||||
import exit msvcrt.dll
|
||||
import printf msvcrt.dll ; tell assembler function is found in library msvcrt.dll
|
||||
|
||||
segment data use32 class=data
|
||||
|
||||
|
||||
segment code use32 class=code
|
||||
start:
|
||||
; will calculate 20 + 123 + 7 in EAX
|
||||
mov al, 250>>4
|
||||
mov al,0ffffh>>4
|
||||
mov al,0efffh>>12
|
||||
mov al,-1>>4
|
||||
mov al,-1>>12
|
||||
push dword 0 ; we place on stack parameter for exit
|
||||
call [exit] ; call exit to end the program
|
||||
@@ -0,0 +1,114 @@
|
||||
import itertools
|
||||
import time
|
||||
|
||||
def operation(x,y,R):
|
||||
"""
|
||||
:params:
|
||||
x: int
|
||||
y: int
|
||||
R: list of tuples
|
||||
:return:
|
||||
int
|
||||
|
||||
This function returns the result of the operation R on x and y
|
||||
"""
|
||||
for relation in R:
|
||||
if relation[0]==x and relation[1]==y:
|
||||
return relation[2]
|
||||
|
||||
def associativity_checker(n,R):
|
||||
"""
|
||||
:params:
|
||||
n: int
|
||||
R: list of tuples
|
||||
:return:
|
||||
bool
|
||||
|
||||
This function checks if the operation R is associative or not. It returns True if it is associative and False otherwise.
|
||||
"""
|
||||
for i in range(n):
|
||||
for j in range(n):
|
||||
for k in range(n):
|
||||
if operation(operation(i,j,R),k,R)!=operation(i,operation(j,k,R),R):
|
||||
return False
|
||||
return True
|
||||
|
||||
def generate_all_operations(n):
|
||||
"""
|
||||
:params:
|
||||
n: int
|
||||
:return:
|
||||
list of lists of tuples
|
||||
|
||||
This function generates all possible operations on a set of n elements. It returns a list of all possible operations.
|
||||
"""
|
||||
R=[]
|
||||
values = set(range(n))
|
||||
operations_results = itertools.product(values,repeat=n**2)
|
||||
f = open(f"output_n_{n}.txt","w")
|
||||
for operation_result in operations_results:
|
||||
op=[]
|
||||
for i in values:
|
||||
for j in values:
|
||||
op.append((i,j,operation_result[i*n+j]))
|
||||
if associativity_checker(n,op):
|
||||
R.append(op)
|
||||
f.write("\n\n")
|
||||
f.write(' |')
|
||||
for i in range(n):
|
||||
f.write(str(i)+"|")
|
||||
for i in range(n):
|
||||
f.write("\n"+"-+"*(n+1)+"\n")
|
||||
f.write(str(i)+"|")
|
||||
for j in range(n):
|
||||
f.write(str(operation(i,j,op))+"|")
|
||||
f.close()
|
||||
return R
|
||||
|
||||
|
||||
def main():
|
||||
print("Enter the number of elements in the set. Number must be smaller than 4")
|
||||
n=int(input())
|
||||
data = generate_all_operations(n)
|
||||
print(len(data))
|
||||
count=0
|
||||
for i in data:
|
||||
if associativity_checker(n,i):
|
||||
print(i)
|
||||
count+=1
|
||||
print(count)
|
||||
|
||||
def file_handle(n):
|
||||
"""
|
||||
:params:
|
||||
n: int
|
||||
:return:
|
||||
None
|
||||
|
||||
This function generates all possible operations on a set of n elements and writes the results to a file. It also writes the number of associative operations.
|
||||
"""
|
||||
semi_groups = generate_all_operations(n)
|
||||
f = open(f"output_n_{n}.txt","w")
|
||||
f.write("Number of elements in the set: "+str(n)+"\n")
|
||||
f.write("Number of associative operations: "+str(len(semi_groups))+"\n")
|
||||
for op in semi_groups:
|
||||
f.write("\n\n")
|
||||
f.write(' |')
|
||||
for i in range(n):
|
||||
f.write(str(i)+"|")
|
||||
for i in range(n):
|
||||
f.write("\n"+"-+"*(n+1)+"\n")
|
||||
f.write(str(i)+"|")
|
||||
for j in range(n):
|
||||
f.write(str(operation(i,j,op))+"|")
|
||||
f.close()
|
||||
|
||||
|
||||
if __name__=="__main__":
|
||||
# file_handle(1)
|
||||
# file_handle(2)
|
||||
# file_handle(3)
|
||||
# file_handle(4)
|
||||
main()
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
Number of elements in the set: 1
|
||||
Number of associative operations: 1
|
||||
|
||||
|
||||
|0|
|
||||
-+-+
|
||||
0|0|
|
||||
@@ -0,0 +1,51 @@
|
||||
Number of elements in the set: 2
|
||||
Number of associative operations: 8
|
||||
|
||||
|
||||
|0|1|
|
||||
-+-+-+
|
||||
0|0|0|
|
||||
-+-+-+
|
||||
1|0|1|
|
||||
|
||||
|0|1|
|
||||
-+-+-+
|
||||
0|0|1|
|
||||
-+-+-+
|
||||
1|0|1|
|
||||
|
||||
|0|1|
|
||||
-+-+-+
|
||||
0|0|1|
|
||||
-+-+-+
|
||||
1|1|1|
|
||||
|
||||
|0|1|
|
||||
-+-+-+
|
||||
0|0|1|
|
||||
-+-+-+
|
||||
1|1|0|
|
||||
|
||||
|0|1|
|
||||
-+-+-+
|
||||
0|0|0|
|
||||
-+-+-+
|
||||
1|0|0|
|
||||
|
||||
|0|1|
|
||||
-+-+-+
|
||||
0|1|0|
|
||||
-+-+-+
|
||||
1|0|1|
|
||||
|
||||
|0|1|
|
||||
-+-+-+
|
||||
0|0|0|
|
||||
-+-+-+
|
||||
1|1|1|
|
||||
|
||||
|0|1|
|
||||
-+-+-+
|
||||
0|1|1|
|
||||
-+-+-+
|
||||
1|1|1|
|
||||
@@ -0,0 +1,907 @@
|
||||
Number of elements in the set: 3
|
||||
Number of associative operations: 113
|
||||
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|0|1|0|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|2|0|1|
|
||||
-+-+-+-+
|
||||
1|0|1|2|
|
||||
-+-+-+-+
|
||||
2|1|2|0|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|1|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|1|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|0|0|0|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|2|1|2|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|2|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|0|0|0|
|
||||
-+-+-+-+
|
||||
2|0|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|2|
|
||||
-+-+-+-+
|
||||
1|0|1|2|
|
||||
-+-+-+-+
|
||||
2|0|0|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|0|
|
||||
-+-+-+-+
|
||||
1|0|1|1|
|
||||
-+-+-+-+
|
||||
2|0|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|2|
|
||||
-+-+-+-+
|
||||
1|1|2|1|
|
||||
-+-+-+-+
|
||||
2|2|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|2|0|2|
|
||||
-+-+-+-+
|
||||
1|0|1|2|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|2|0|2|
|
||||
-+-+-+-+
|
||||
1|2|1|2|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|1|0|1|
|
||||
-+-+-+-+
|
||||
1|0|1|0|
|
||||
-+-+-+-+
|
||||
2|1|0|1|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|2|2|2|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|2|2|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|1|0|0|
|
||||
-+-+-+-+
|
||||
1|0|1|1|
|
||||
-+-+-+-+
|
||||
2|0|1|1|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|1|1|2|
|
||||
-+-+-+-+
|
||||
1|1|1|2|
|
||||
-+-+-+-+
|
||||
2|2|2|1|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|2|2|2|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|2|
|
||||
-+-+-+-+
|
||||
1|1|1|2|
|
||||
-+-+-+-+
|
||||
2|2|2|1|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|1|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|2|1|0|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|0|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|2|
|
||||
-+-+-+-+
|
||||
1|0|1|2|
|
||||
-+-+-+-+
|
||||
2|2|2|0|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|2|2|0|
|
||||
-+-+-+-+
|
||||
1|2|2|0|
|
||||
-+-+-+-+
|
||||
2|0|0|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|2|
|
||||
-+-+-+-+
|
||||
1|1|0|2|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|0|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|0|1|0|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|2|
|
||||
-+-+-+-+
|
||||
1|1|2|2|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|1|2|1|
|
||||
-+-+-+-+
|
||||
1|2|1|2|
|
||||
-+-+-+-+
|
||||
2|1|2|1|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|0|
|
||||
-+-+-+-+
|
||||
1|1|0|1|
|
||||
-+-+-+-+
|
||||
2|0|1|0|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|2|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|1|1|1|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|2|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|2|1|0|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|1|1|1|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|0|2|1|
|
||||
-+-+-+-+
|
||||
2|0|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|1|1|2|
|
||||
-+-+-+-+
|
||||
1|1|1|2|
|
||||
-+-+-+-+
|
||||
2|1|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|0|1|0|
|
||||
-+-+-+-+
|
||||
2|0|0|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|1|2|2|
|
||||
-+-+-+-+
|
||||
1|2|2|2|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|1|1|2|
|
||||
-+-+-+-+
|
||||
1|1|1|2|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|0|0|1|
|
||||
-+-+-+-+
|
||||
2|0|0|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|0|2|0|
|
||||
-+-+-+-+
|
||||
2|0|0|0|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|2|1|2|
|
||||
-+-+-+-+
|
||||
1|1|2|1|
|
||||
-+-+-+-+
|
||||
2|2|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|2|0|0|
|
||||
-+-+-+-+
|
||||
1|0|1|2|
|
||||
-+-+-+-+
|
||||
2|0|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|2|2|
|
||||
-+-+-+-+
|
||||
1|2|2|2|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|2|
|
||||
-+-+-+-+
|
||||
1|0|0|2|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|2|2|
|
||||
-+-+-+-+
|
||||
1|1|2|2|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|1|2|0|
|
||||
-+-+-+-+
|
||||
1|2|0|1|
|
||||
-+-+-+-+
|
||||
2|0|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|2|
|
||||
-+-+-+-+
|
||||
1|1|1|2|
|
||||
-+-+-+-+
|
||||
2|2|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|2|0|0|
|
||||
-+-+-+-+
|
||||
1|0|2|2|
|
||||
-+-+-+-+
|
||||
2|0|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|1|0|0|
|
||||
-+-+-+-+
|
||||
1|0|1|1|
|
||||
-+-+-+-+
|
||||
2|0|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|2|
|
||||
-+-+-+-+
|
||||
1|1|1|2|
|
||||
-+-+-+-+
|
||||
2|1|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|2|
|
||||
-+-+-+-+
|
||||
1|1|2|0|
|
||||
-+-+-+-+
|
||||
2|2|0|1|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|1|1|1|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|1|1|0|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|2|2|2|
|
||||
-+-+-+-+
|
||||
1|2|1|2|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|0|1|1|
|
||||
-+-+-+-+
|
||||
2|0|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|2|2|2|
|
||||
-+-+-+-+
|
||||
1|2|2|2|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|1|0|2|
|
||||
-+-+-+-+
|
||||
1|0|1|2|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|2|
|
||||
-+-+-+-+
|
||||
1|1|1|2|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|2|
|
||||
-+-+-+-+
|
||||
1|2|1|2|
|
||||
-+-+-+-+
|
||||
2|2|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|1|
|
||||
-+-+-+-+
|
||||
1|1|0|0|
|
||||
-+-+-+-+
|
||||
2|1|0|0|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|0|0|0|
|
||||
-+-+-+-+
|
||||
2|0|0|0|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|0|1|2|
|
||||
-+-+-+-+
|
||||
2|0|2|0|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|0|
|
||||
-+-+-+-+
|
||||
1|0|1|0|
|
||||
-+-+-+-+
|
||||
2|0|1|0|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|2|
|
||||
-+-+-+-+
|
||||
1|0|0|2|
|
||||
-+-+-+-+
|
||||
2|0|0|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|2|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|0|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|0|0|1|
|
||||
-+-+-+-+
|
||||
2|0|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|0|1|0|
|
||||
-+-+-+-+
|
||||
2|0|2|0|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|2|
|
||||
-+-+-+-+
|
||||
1|2|2|2|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|0|2|2|
|
||||
-+-+-+-+
|
||||
2|0|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|0|0|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|2|1|2|
|
||||
-+-+-+-+
|
||||
1|2|1|2|
|
||||
-+-+-+-+
|
||||
2|2|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|2|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|2|1|1|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|2|2|
|
||||
-+-+-+-+
|
||||
1|2|0|0|
|
||||
-+-+-+-+
|
||||
2|2|0|0|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|1|
|
||||
-+-+-+-+
|
||||
1|0|1|1|
|
||||
-+-+-+-+
|
||||
2|0|1|1|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|1|1|0|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|1|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|1|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|1|1|0|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|0|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|0|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|0|0|0|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|1|1|1|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|1|1|1|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|2|
|
||||
-+-+-+-+
|
||||
1|0|1|2|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|0|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|2|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|2|2|
|
||||
-+-+-+-+
|
||||
1|0|2|2|
|
||||
-+-+-+-+
|
||||
2|0|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|2|2|1|
|
||||
-+-+-+-+
|
||||
1|2|2|1|
|
||||
-+-+-+-+
|
||||
2|1|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|0|1|1|
|
||||
-+-+-+-+
|
||||
2|0|1|1|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|0|1|2|
|
||||
-+-+-+-+
|
||||
2|0|2|1|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|2|
|
||||
-+-+-+-+
|
||||
1|0|1|2|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|2|2|
|
||||
-+-+-+-+
|
||||
1|2|1|2|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|0|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|0|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|0|
|
||||
-+-+-+-+
|
||||
1|1|0|1|
|
||||
-+-+-+-+
|
||||
2|0|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|2|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|2|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|2|
|
||||
-+-+-+-+
|
||||
1|0|1|2|
|
||||
-+-+-+-+
|
||||
2|0|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|0|0|0|
|
||||
-+-+-+-+
|
||||
2|0|0|1|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|1|1|1|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|0|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|2|
|
||||
-+-+-+-+
|
||||
1|0|1|2|
|
||||
-+-+-+-+
|
||||
2|0|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|2|1|1|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|1|1|1|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|0|1|0|
|
||||
-+-+-+-+
|
||||
2|0|0|0|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|2|2|2|
|
||||
-+-+-+-+
|
||||
1|2|0|2|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|1|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|1|1|1|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|2|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|2|2|2|
|
||||
-+-+-+-+
|
||||
1|0|1|2|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|0|1|2|
|
||||
-+-+-+-+
|
||||
2|0|0|0|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|1|1|1|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|2|
|
||||
-+-+-+-+
|
||||
1|0|0|2|
|
||||
-+-+-+-+
|
||||
2|2|2|0|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|1|
|
||||
-+-+-+-+
|
||||
1|0|1|1|
|
||||
-+-+-+-+
|
||||
2|0|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|0|1|1|
|
||||
-+-+-+-+
|
||||
2|0|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|1|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|2|1|1|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|0|1|2|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|0|1|2|
|
||||
-+-+-+-+
|
||||
2|0|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|1|1|1|
|
||||
-+-+-+-+
|
||||
1|1|1|1|
|
||||
-+-+-+-+
|
||||
2|1|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|0|0|0|
|
||||
-+-+-+-+
|
||||
2|0|0|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|0|1|2|
|
||||
-+-+-+-+
|
||||
2|0|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|0|
|
||||
-+-+-+-+
|
||||
1|2|1|2|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|0|2|
|
||||
-+-+-+-+
|
||||
1|1|1|2|
|
||||
-+-+-+-+
|
||||
2|2|2|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|1|0|
|
||||
-+-+-+-+
|
||||
1|0|1|0|
|
||||
-+-+-+-+
|
||||
2|0|1|2|
|
||||
|
||||
|0|1|2|
|
||||
-+-+-+-+
|
||||
0|0|2|2|
|
||||
-+-+-+-+
|
||||
1|0|1|2|
|
||||
-+-+-+-+
|
||||
2|0|2|2|
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,104 @@
|
||||
import itertools
|
||||
import numpy as np
|
||||
|
||||
class Vector:
|
||||
"""
|
||||
This class represents a vector in Z2^n and has the following methods:
|
||||
__init__(self, *args): This method initializes the vector with the values passed as arguments.
|
||||
__add__(self, other): This method returns the sum of two vectors.
|
||||
__mul__(self, value): This method returns the product of a vector and a scalar.
|
||||
__str__(self): This method returns the string representation of the vector.
|
||||
__repr__(self): This method returns the string representation of the vector.
|
||||
dereference(self): This method returns the values of the vector.
|
||||
"""
|
||||
def __init__(self, *args):
|
||||
self.values = args
|
||||
def __add__(self, other):
|
||||
return Vector(*[(x + y)%2 for x, y in zip(self.values, other.values)])
|
||||
def __mul__(self, value):
|
||||
if value == 0 or value == 1:
|
||||
return Vector(*[x * value for x in self.values])
|
||||
else:
|
||||
raise ValueError("The value must be 0 or 1")
|
||||
def __str__(self):
|
||||
return str(self.values)
|
||||
def __repr__(self):
|
||||
return str(self.values)
|
||||
def __eq__(self, __o: object) -> bool:
|
||||
for i in range(len(self.values)):
|
||||
if self.values[i] != __o.values[i]:
|
||||
return False
|
||||
return True
|
||||
def dereference(self):
|
||||
return self.values
|
||||
|
||||
def generate_all_vectors(n):
|
||||
"""
|
||||
:params:
|
||||
n: int
|
||||
:return:
|
||||
list of Vector objects
|
||||
This function generates all possible vectors in Z2^n and returns a list of all possible vectors.
|
||||
"""
|
||||
return [Vector(*x) for x in itertools.product([0, 1], repeat=n)]
|
||||
|
||||
def generate_all_bases(n):
|
||||
"""
|
||||
:params:
|
||||
n: int
|
||||
:return:
|
||||
list of lists of Vector objects
|
||||
This function generates all possible bases of Z2^n and returns a list of all possible bases.
|
||||
"""
|
||||
possible_bases = list(itertools.product(generate_all_vectors(n), repeat=n))
|
||||
bases = []
|
||||
for base in possible_bases:
|
||||
if is_linearly_independent(base):
|
||||
bases.append(base)
|
||||
return bases
|
||||
|
||||
def is_linearly_independent(vectors):
|
||||
"""
|
||||
:params:
|
||||
vectors: list of Vector objects
|
||||
:return:
|
||||
bool
|
||||
This function checks if the vectors passed as arguments are linearly independent or not. It returns True if they are linearly independent and False otherwise.
|
||||
"""
|
||||
z2=[0,1]
|
||||
for scalar in list(itertools.product(z2, repeat=len(vectors))):
|
||||
if scalar == tuple([0 for i in range(len(vectors))]):
|
||||
continue
|
||||
vector = Vector(*[0 for i in range(len(vectors[0].dereference()))])
|
||||
for i in range(len(vectors)):
|
||||
vector += vectors[i] * scalar[i]
|
||||
if vector == Vector(*[0 for i in range(len(vectors[0].dereference()))]):
|
||||
return False
|
||||
return True
|
||||
|
||||
def main():
|
||||
n = int(input("Enter the dimension of the vector space: "))
|
||||
bases = generate_all_bases(n)
|
||||
print("The bases are: {}".format(bases))
|
||||
print("The number of bases for the vector space is: {}".format(len(bases)))
|
||||
|
||||
def file_handle(n):
|
||||
"""
|
||||
:params:
|
||||
n: int
|
||||
This function generates all possible bases of Z2^n and writes them to a file.
|
||||
The file is named output_n_n.txt where n is the dimension of the vector space.
|
||||
"""
|
||||
bases = generate_all_bases(n)
|
||||
with open(f"output_n_{n}.txt", "w") as f:
|
||||
f.write("The number of bases for the vector space Z2^{} is: {}\n".format(n,len(bases)))
|
||||
f.write("The bases are:\n")
|
||||
for base in bases:
|
||||
f.write(str(base) + "\n")
|
||||
|
||||
if __name__ == "__main__":
|
||||
file_handle(1)
|
||||
file_handle(2)
|
||||
file_handle(3)
|
||||
file_handle(4)
|
||||
# main()
|
||||
@@ -0,0 +1,3 @@
|
||||
The number of bases for the vector space Z2^1 is: 1
|
||||
The bases are:
|
||||
((1,),)
|
||||
@@ -0,0 +1,8 @@
|
||||
The number of bases for the vector space Z2^2 is: 6
|
||||
The bases are:
|
||||
((0, 1), (1, 0))
|
||||
((0, 1), (1, 1))
|
||||
((1, 0), (0, 1))
|
||||
((1, 0), (1, 1))
|
||||
((1, 1), (0, 1))
|
||||
((1, 1), (1, 0))
|
||||
@@ -0,0 +1,170 @@
|
||||
The number of bases for the vector space Z2^3 is: 168
|
||||
The bases are:
|
||||
((0, 0, 1), (0, 1, 0), (1, 0, 0))
|
||||
((0, 0, 1), (0, 1, 0), (1, 0, 1))
|
||||
((0, 0, 1), (0, 1, 0), (1, 1, 0))
|
||||
((0, 0, 1), (0, 1, 0), (1, 1, 1))
|
||||
((0, 0, 1), (0, 1, 1), (1, 0, 0))
|
||||
((0, 0, 1), (0, 1, 1), (1, 0, 1))
|
||||
((0, 0, 1), (0, 1, 1), (1, 1, 0))
|
||||
((0, 0, 1), (0, 1, 1), (1, 1, 1))
|
||||
((0, 0, 1), (1, 0, 0), (0, 1, 0))
|
||||
((0, 0, 1), (1, 0, 0), (0, 1, 1))
|
||||
((0, 0, 1), (1, 0, 0), (1, 1, 0))
|
||||
((0, 0, 1), (1, 0, 0), (1, 1, 1))
|
||||
((0, 0, 1), (1, 0, 1), (0, 1, 0))
|
||||
((0, 0, 1), (1, 0, 1), (0, 1, 1))
|
||||
((0, 0, 1), (1, 0, 1), (1, 1, 0))
|
||||
((0, 0, 1), (1, 0, 1), (1, 1, 1))
|
||||
((0, 0, 1), (1, 1, 0), (0, 1, 0))
|
||||
((0, 0, 1), (1, 1, 0), (0, 1, 1))
|
||||
((0, 0, 1), (1, 1, 0), (1, 0, 0))
|
||||
((0, 0, 1), (1, 1, 0), (1, 0, 1))
|
||||
((0, 0, 1), (1, 1, 1), (0, 1, 0))
|
||||
((0, 0, 1), (1, 1, 1), (0, 1, 1))
|
||||
((0, 0, 1), (1, 1, 1), (1, 0, 0))
|
||||
((0, 0, 1), (1, 1, 1), (1, 0, 1))
|
||||
((0, 1, 0), (0, 0, 1), (1, 0, 0))
|
||||
((0, 1, 0), (0, 0, 1), (1, 0, 1))
|
||||
((0, 1, 0), (0, 0, 1), (1, 1, 0))
|
||||
((0, 1, 0), (0, 0, 1), (1, 1, 1))
|
||||
((0, 1, 0), (0, 1, 1), (1, 0, 0))
|
||||
((0, 1, 0), (0, 1, 1), (1, 0, 1))
|
||||
((0, 1, 0), (0, 1, 1), (1, 1, 0))
|
||||
((0, 1, 0), (0, 1, 1), (1, 1, 1))
|
||||
((0, 1, 0), (1, 0, 0), (0, 0, 1))
|
||||
((0, 1, 0), (1, 0, 0), (0, 1, 1))
|
||||
((0, 1, 0), (1, 0, 0), (1, 0, 1))
|
||||
((0, 1, 0), (1, 0, 0), (1, 1, 1))
|
||||
((0, 1, 0), (1, 0, 1), (0, 0, 1))
|
||||
((0, 1, 0), (1, 0, 1), (0, 1, 1))
|
||||
((0, 1, 0), (1, 0, 1), (1, 0, 0))
|
||||
((0, 1, 0), (1, 0, 1), (1, 1, 0))
|
||||
((0, 1, 0), (1, 1, 0), (0, 0, 1))
|
||||
((0, 1, 0), (1, 1, 0), (0, 1, 1))
|
||||
((0, 1, 0), (1, 1, 0), (1, 0, 1))
|
||||
((0, 1, 0), (1, 1, 0), (1, 1, 1))
|
||||
((0, 1, 0), (1, 1, 1), (0, 0, 1))
|
||||
((0, 1, 0), (1, 1, 1), (0, 1, 1))
|
||||
((0, 1, 0), (1, 1, 1), (1, 0, 0))
|
||||
((0, 1, 0), (1, 1, 1), (1, 1, 0))
|
||||
((0, 1, 1), (0, 0, 1), (1, 0, 0))
|
||||
((0, 1, 1), (0, 0, 1), (1, 0, 1))
|
||||
((0, 1, 1), (0, 0, 1), (1, 1, 0))
|
||||
((0, 1, 1), (0, 0, 1), (1, 1, 1))
|
||||
((0, 1, 1), (0, 1, 0), (1, 0, 0))
|
||||
((0, 1, 1), (0, 1, 0), (1, 0, 1))
|
||||
((0, 1, 1), (0, 1, 0), (1, 1, 0))
|
||||
((0, 1, 1), (0, 1, 0), (1, 1, 1))
|
||||
((0, 1, 1), (1, 0, 0), (0, 0, 1))
|
||||
((0, 1, 1), (1, 0, 0), (0, 1, 0))
|
||||
((0, 1, 1), (1, 0, 0), (1, 0, 1))
|
||||
((0, 1, 1), (1, 0, 0), (1, 1, 0))
|
||||
((0, 1, 1), (1, 0, 1), (0, 0, 1))
|
||||
((0, 1, 1), (1, 0, 1), (0, 1, 0))
|
||||
((0, 1, 1), (1, 0, 1), (1, 0, 0))
|
||||
((0, 1, 1), (1, 0, 1), (1, 1, 1))
|
||||
((0, 1, 1), (1, 1, 0), (0, 0, 1))
|
||||
((0, 1, 1), (1, 1, 0), (0, 1, 0))
|
||||
((0, 1, 1), (1, 1, 0), (1, 0, 0))
|
||||
((0, 1, 1), (1, 1, 0), (1, 1, 1))
|
||||
((0, 1, 1), (1, 1, 1), (0, 0, 1))
|
||||
((0, 1, 1), (1, 1, 1), (0, 1, 0))
|
||||
((0, 1, 1), (1, 1, 1), (1, 0, 1))
|
||||
((0, 1, 1), (1, 1, 1), (1, 1, 0))
|
||||
((1, 0, 0), (0, 0, 1), (0, 1, 0))
|
||||
((1, 0, 0), (0, 0, 1), (0, 1, 1))
|
||||
((1, 0, 0), (0, 0, 1), (1, 1, 0))
|
||||
((1, 0, 0), (0, 0, 1), (1, 1, 1))
|
||||
((1, 0, 0), (0, 1, 0), (0, 0, 1))
|
||||
((1, 0, 0), (0, 1, 0), (0, 1, 1))
|
||||
((1, 0, 0), (0, 1, 0), (1, 0, 1))
|
||||
((1, 0, 0), (0, 1, 0), (1, 1, 1))
|
||||
((1, 0, 0), (0, 1, 1), (0, 0, 1))
|
||||
((1, 0, 0), (0, 1, 1), (0, 1, 0))
|
||||
((1, 0, 0), (0, 1, 1), (1, 0, 1))
|
||||
((1, 0, 0), (0, 1, 1), (1, 1, 0))
|
||||
((1, 0, 0), (1, 0, 1), (0, 1, 0))
|
||||
((1, 0, 0), (1, 0, 1), (0, 1, 1))
|
||||
((1, 0, 0), (1, 0, 1), (1, 1, 0))
|
||||
((1, 0, 0), (1, 0, 1), (1, 1, 1))
|
||||
((1, 0, 0), (1, 1, 0), (0, 0, 1))
|
||||
((1, 0, 0), (1, 1, 0), (0, 1, 1))
|
||||
((1, 0, 0), (1, 1, 0), (1, 0, 1))
|
||||
((1, 0, 0), (1, 1, 0), (1, 1, 1))
|
||||
((1, 0, 0), (1, 1, 1), (0, 0, 1))
|
||||
((1, 0, 0), (1, 1, 1), (0, 1, 0))
|
||||
((1, 0, 0), (1, 1, 1), (1, 0, 1))
|
||||
((1, 0, 0), (1, 1, 1), (1, 1, 0))
|
||||
((1, 0, 1), (0, 0, 1), (0, 1, 0))
|
||||
((1, 0, 1), (0, 0, 1), (0, 1, 1))
|
||||
((1, 0, 1), (0, 0, 1), (1, 1, 0))
|
||||
((1, 0, 1), (0, 0, 1), (1, 1, 1))
|
||||
((1, 0, 1), (0, 1, 0), (0, 0, 1))
|
||||
((1, 0, 1), (0, 1, 0), (0, 1, 1))
|
||||
((1, 0, 1), (0, 1, 0), (1, 0, 0))
|
||||
((1, 0, 1), (0, 1, 0), (1, 1, 0))
|
||||
((1, 0, 1), (0, 1, 1), (0, 0, 1))
|
||||
((1, 0, 1), (0, 1, 1), (0, 1, 0))
|
||||
((1, 0, 1), (0, 1, 1), (1, 0, 0))
|
||||
((1, 0, 1), (0, 1, 1), (1, 1, 1))
|
||||
((1, 0, 1), (1, 0, 0), (0, 1, 0))
|
||||
((1, 0, 1), (1, 0, 0), (0, 1, 1))
|
||||
((1, 0, 1), (1, 0, 0), (1, 1, 0))
|
||||
((1, 0, 1), (1, 0, 0), (1, 1, 1))
|
||||
((1, 0, 1), (1, 1, 0), (0, 0, 1))
|
||||
((1, 0, 1), (1, 1, 0), (0, 1, 0))
|
||||
((1, 0, 1), (1, 1, 0), (1, 0, 0))
|
||||
((1, 0, 1), (1, 1, 0), (1, 1, 1))
|
||||
((1, 0, 1), (1, 1, 1), (0, 0, 1))
|
||||
((1, 0, 1), (1, 1, 1), (0, 1, 1))
|
||||
((1, 0, 1), (1, 1, 1), (1, 0, 0))
|
||||
((1, 0, 1), (1, 1, 1), (1, 1, 0))
|
||||
((1, 1, 0), (0, 0, 1), (0, 1, 0))
|
||||
((1, 1, 0), (0, 0, 1), (0, 1, 1))
|
||||
((1, 1, 0), (0, 0, 1), (1, 0, 0))
|
||||
((1, 1, 0), (0, 0, 1), (1, 0, 1))
|
||||
((1, 1, 0), (0, 1, 0), (0, 0, 1))
|
||||
((1, 1, 0), (0, 1, 0), (0, 1, 1))
|
||||
((1, 1, 0), (0, 1, 0), (1, 0, 1))
|
||||
((1, 1, 0), (0, 1, 0), (1, 1, 1))
|
||||
((1, 1, 0), (0, 1, 1), (0, 0, 1))
|
||||
((1, 1, 0), (0, 1, 1), (0, 1, 0))
|
||||
((1, 1, 0), (0, 1, 1), (1, 0, 0))
|
||||
((1, 1, 0), (0, 1, 1), (1, 1, 1))
|
||||
((1, 1, 0), (1, 0, 0), (0, 0, 1))
|
||||
((1, 1, 0), (1, 0, 0), (0, 1, 1))
|
||||
((1, 1, 0), (1, 0, 0), (1, 0, 1))
|
||||
((1, 1, 0), (1, 0, 0), (1, 1, 1))
|
||||
((1, 1, 0), (1, 0, 1), (0, 0, 1))
|
||||
((1, 1, 0), (1, 0, 1), (0, 1, 0))
|
||||
((1, 1, 0), (1, 0, 1), (1, 0, 0))
|
||||
((1, 1, 0), (1, 0, 1), (1, 1, 1))
|
||||
((1, 1, 0), (1, 1, 1), (0, 1, 0))
|
||||
((1, 1, 0), (1, 1, 1), (0, 1, 1))
|
||||
((1, 1, 0), (1, 1, 1), (1, 0, 0))
|
||||
((1, 1, 0), (1, 1, 1), (1, 0, 1))
|
||||
((1, 1, 1), (0, 0, 1), (0, 1, 0))
|
||||
((1, 1, 1), (0, 0, 1), (0, 1, 1))
|
||||
((1, 1, 1), (0, 0, 1), (1, 0, 0))
|
||||
((1, 1, 1), (0, 0, 1), (1, 0, 1))
|
||||
((1, 1, 1), (0, 1, 0), (0, 0, 1))
|
||||
((1, 1, 1), (0, 1, 0), (0, 1, 1))
|
||||
((1, 1, 1), (0, 1, 0), (1, 0, 0))
|
||||
((1, 1, 1), (0, 1, 0), (1, 1, 0))
|
||||
((1, 1, 1), (0, 1, 1), (0, 0, 1))
|
||||
((1, 1, 1), (0, 1, 1), (0, 1, 0))
|
||||
((1, 1, 1), (0, 1, 1), (1, 0, 1))
|
||||
((1, 1, 1), (0, 1, 1), (1, 1, 0))
|
||||
((1, 1, 1), (1, 0, 0), (0, 0, 1))
|
||||
((1, 1, 1), (1, 0, 0), (0, 1, 0))
|
||||
((1, 1, 1), (1, 0, 0), (1, 0, 1))
|
||||
((1, 1, 1), (1, 0, 0), (1, 1, 0))
|
||||
((1, 1, 1), (1, 0, 1), (0, 0, 1))
|
||||
((1, 1, 1), (1, 0, 1), (0, 1, 1))
|
||||
((1, 1, 1), (1, 0, 1), (1, 0, 0))
|
||||
((1, 1, 1), (1, 0, 1), (1, 1, 0))
|
||||
((1, 1, 1), (1, 1, 0), (0, 1, 0))
|
||||
((1, 1, 1), (1, 1, 0), (0, 1, 1))
|
||||
((1, 1, 1), (1, 1, 0), (1, 0, 0))
|
||||
((1, 1, 1), (1, 1, 0), (1, 0, 1))
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
@@ -0,0 +1,129 @@
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
pip-wheel-metadata/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
.python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
#Pipfile.lock
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
@@ -0,0 +1,2 @@
|
||||
# FP
|
||||
Fundamentals of Programming code samples for 2022/2023 school year.
|
||||
@@ -0,0 +1,32 @@
|
||||
# :computer: Assignment 01
|
||||
|
||||
## Requirements
|
||||
- Solve one problem statement from each set
|
||||
- Write the solution to each problem statement in its corresponding Python module (`p1.py`, `p2.py` and `p3.py` respectively).
|
||||
- Use functions, input parameters and return values
|
||||
- Each function only does one thing!
|
||||
- Do not use global variables
|
||||
- Provide the user relevant messages regarding expected input and the meaning of the program’s output.
|
||||
- Assignment should be **completed by week 2, hard deadline is week 3**.
|
||||
|
||||
## Problem Statements
|
||||
### First Set
|
||||
1. Generate the first prime number larger than a given natural number `n`.
|
||||
2. Given natural number `n`, determine the prime numbers `p1` and `p2` such that `n = p1 + p2` (check the Goldbach hypothesis).
|
||||
3. For a given natural number `n` find the minimal natural number `m` formed with the same digits. (e.g. `n=3658, m=3568`).
|
||||
4. For a given natural number `n` find the largest natural number written with the same digits. (e.g. `n=3658, m=8653`).
|
||||
5. Generate the largest prime number smaller than a given natural number `n`. If such a number does not exist, a message should be displayed.
|
||||
|
||||
### Second Set
|
||||
6. Determine a calendar date (as year, month, day) starting from two integer numbers representing the year and the day number inside that year (e.g. day number 32 is February 1st). Take into account leap years. Do not use Python's inbuilt date/time functions.
|
||||
7. Determine the twin prime numbers `p1` and `p2` immediately larger than the given non-null natural number `n`. Two prime numbers `p` and `q` are called twin if `q - p = 2`.
|
||||
8. Find the smallest number `m` from the Fibonacci sequence, defined by `f[0]=f[1]=1`, `f[n]=f[n-1] + f[n-2]`, for `n > 2`, larger than the given natural number `n`. (e.g. `for n = 6, m = 8`).
|
||||
9. Consider a given natural number `n`. Determine the product `p` of all the proper factors of `n`.
|
||||
10. The palindrome of a number is the number obtained by reversing the order of its digits (e.g. the `palindrome of 237 is 732`). For a given natural number `n`, determine its palindrome.
|
||||
11. The numbers `n1` and `n2` have the property `P` if their writing in base 10 uses the same digits (e.g. `2113 and 323121`). Determine whether two given natural numbers have property `P`.
|
||||
|
||||
### Third Set
|
||||
12. Determine the age of a person, in number of days. Take into account leap years, as well as the date of birth and current date `(year, month, day)`. Do not use Python's inbuilt date/time functions.
|
||||
13. Determine the `n-th` element of the sequence `1,2,3,2,5,2,3,7,2,3,2,5,...` obtained from the sequence of natural numbers by replacing composed numbers with their prime divisors, without memorizing the elements of the sequence.
|
||||
14. Determine the `n-th` element of the sequence `1,2,3,2,2,5,2,2,3,3,3,7,2,2,3,3,3,...` obtained from the sequence of natural numbers by replacing composed numbers with their prime divisors, each divisor `d` being written `d` times, without memorizing the elements of the sequence.
|
||||
15. Generate the largest perfect number smaller than a given natural number `n`. If such a number does not exist, a message should be displayed. A number is perfect if it is equal to the sum of its divisors, except itself. (e.g. `6 is a perfect number, as 6=1+2+3`).
|
||||
@@ -0,0 +1,32 @@
|
||||
# Assignment 02
|
||||
|
||||
## Requirements
|
||||
Implement a menu-driven console application to help visualize the way sorting algorithms work. You will be given two of the algorithms from the list below to implement (one from each set). When started, the program will print a menu with the following options:
|
||||
- Generate a list of `n` random natural numbers. Generated numbers must be between `0` and `100`.
|
||||
- Sort the list using the first algorithm. (see **NB!** below)
|
||||
- Sort the list using the second algorithm. (see **NB!** below)
|
||||
- Exit the program
|
||||
|
||||
## NB!
|
||||
Before starting each sort, the program will ask for the value of parameter `step`. During sorting, the program will display the partially sorted list on the console each time it makes `step` operations or passes, depending on the algorithm (e.g., if `step=2`, display the partially sorted list after each 2 element swaps in bubble sort, after each 2 element insertions in insert sort, after every 2nd generation of a permutation in permutation sort etc.).
|
||||
|
||||
## Implementation requirements
|
||||
- Write a function for each sorting algorithm; each function should take as parameter the list to be sorted and the value of parameter `step` that was read from the console.
|
||||
- Functions communicate using input parameter(s) and return values (**DO NOT use global, or module-level variables**)
|
||||
- Provide the user with a menu-driven console-based user interface. Input data should be read from the console and the results printed to the console. At each step, the program must provide the user the context of the operation (never display an empty prompt).
|
||||
- You may use Internet resources to research the sorting algorithm, but you must be able to explain **how** and **why** they work in detail
|
||||
|
||||
## Sorting algorithms
|
||||
### Basic set
|
||||
- Bubble Sort
|
||||
- Cocktail Sort
|
||||
- Insert Sort
|
||||
- Permutation Sort
|
||||
- Selection Sort
|
||||
|
||||
### Advanced set
|
||||
- Shell Sort
|
||||
- Comb Sort
|
||||
- Gnome Sort
|
||||
- Stooge Sort
|
||||
- Strand Sort
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
"""
|
||||
Created on Sep 23, 2016
|
||||
|
||||
@author: Arthur
|
||||
"""
|
||||
|
||||
"""
|
||||
Print a message to the console
|
||||
"""
|
||||
print("Hello world!")
|
||||
|
||||
"""
|
||||
Read from the console
|
||||
"""
|
||||
a = input("Enter the first number: ")
|
||||
b = input("Enter the second number: ")
|
||||
|
||||
"""
|
||||
NB!
|
||||
What is printed out and why?
|
||||
"""
|
||||
print(a + b)
|
||||
|
||||
# This is a line comment
|
||||
|
||||
"""
|
||||
NB!
|
||||
What does this do?
|
||||
"""
|
||||
x = int(a)
|
||||
y = int(b)
|
||||
|
||||
print(x + y)
|
||||
|
||||
"""
|
||||
NB!
|
||||
This is all very confusing... how do I know what is what?
|
||||
"""
|
||||
print("this should be a string - ", type(a))
|
||||
print("and this is an integer -", type(x))
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
"""
|
||||
Created on Sep 26, 2016
|
||||
|
||||
@author: Arthur
|
||||
"""
|
||||
|
||||
"""
|
||||
List
|
||||
"""
|
||||
|
||||
myList = [1, 2, 3]
|
||||
print(myList)
|
||||
|
||||
print(myList[1])
|
||||
|
||||
print('The list has', len(myList), 'elements')
|
||||
print('Tha first element is', myList[0], 'and the last one is', myList[len(myList) - 1])
|
||||
|
||||
x = myList
|
||||
print(myList, x)
|
||||
|
||||
"""
|
||||
What happens here?
|
||||
"""
|
||||
x[1] = '?'
|
||||
print(myList, x)
|
||||
|
||||
"""
|
||||
List slicing
|
||||
"""
|
||||
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
print(myList[:2])
|
||||
print(myList[2:])
|
||||
myList[5:] = ['a', 'b', 'c']
|
||||
print(myList)
|
||||
|
||||
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
myList[1:9] = 'x'
|
||||
print(myList)
|
||||
|
||||
"""
|
||||
Tuple
|
||||
"""
|
||||
tup = 1, 2, 'a'
|
||||
print(tup)
|
||||
print(tup[1])
|
||||
|
||||
for e in tup:
|
||||
print(e)
|
||||
|
||||
"""
|
||||
What happens if we uncomment this line?
|
||||
"""
|
||||
# tup[1] = 'x'
|
||||
|
||||
"""
|
||||
Dictionary
|
||||
"""
|
||||
d = {'num': 1, 'den': 2}
|
||||
print(d)
|
||||
|
||||
print(d['num'])
|
||||
d['num'] = 99
|
||||
print(d['num'])
|
||||
|
||||
if 'num' in d:
|
||||
print('We have num!')
|
||||
|
||||
del d['num']
|
||||
|
||||
if 'num' in d:
|
||||
print('We have num!')
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
"""
|
||||
Created on Sep 23, 2016
|
||||
|
||||
@author: Arthur
|
||||
"""
|
||||
|
||||
"""
|
||||
Read a number and check whether it is prime
|
||||
"""
|
||||
x = input("Give the number: ")
|
||||
x = int(x)
|
||||
|
||||
isPrime = True
|
||||
for i in range(2, x // 2):
|
||||
if x % i == 0:
|
||||
isPrime = False
|
||||
|
||||
if isPrime:
|
||||
print("Number is prime!")
|
||||
else:
|
||||
print("Number is not prime!")
|
||||
|
||||
"""
|
||||
NB!
|
||||
Let's break program flow as soon as we know it's not a prime
|
||||
"""
|
||||
x = input("Give the number: ")
|
||||
x = int(x)
|
||||
|
||||
isPrime = True
|
||||
i = 2
|
||||
while isPrime and i <= x // 2:
|
||||
i += 1
|
||||
if x % i == 0:
|
||||
isPrime = False
|
||||
|
||||
if isPrime:
|
||||
print("Number is prime!")
|
||||
else:
|
||||
print("Number is not prime!")
|
||||
|
||||
"""
|
||||
Open question!
|
||||
How can you check the functions above do what they are supposed to?
|
||||
"""
|
||||
+153
@@ -0,0 +1,153 @@
|
||||
"""
|
||||
Created on Dec 6, 2016
|
||||
|
||||
@author: Arthur
|
||||
"""
|
||||
|
||||
'''
|
||||
1. Compute the factorial for a given positive integer
|
||||
'''
|
||||
|
||||
|
||||
def factorial(n):
|
||||
"""
|
||||
Determine the factorial for the given positive integer
|
||||
input:
|
||||
n - input parameter
|
||||
output:
|
||||
n!
|
||||
"""
|
||||
|
||||
'''
|
||||
This is the best case, no recursion
|
||||
'''
|
||||
if n == 0:
|
||||
return 1
|
||||
|
||||
'''
|
||||
Recursive step progresses toward the simple case
|
||||
'''
|
||||
return n * factorial(n - 1)
|
||||
|
||||
|
||||
def test_factorial():
|
||||
for n in range(0, 10):
|
||||
fact1 = factorial(n)
|
||||
|
||||
fact2 = 1
|
||||
for i in range(1, n + 1):
|
||||
fact2 *= i
|
||||
|
||||
assert fact1 == fact2
|
||||
|
||||
|
||||
test_factorial()
|
||||
|
||||
'''
|
||||
2. Compute the sum of a list of numbers
|
||||
'''
|
||||
|
||||
|
||||
def sum_list(lst):
|
||||
"""
|
||||
Calculate the sum of the elements in the list
|
||||
input:
|
||||
lst - the list
|
||||
output:
|
||||
The sum of the elements
|
||||
"""
|
||||
|
||||
'''
|
||||
This is the best case, no recursion
|
||||
'''
|
||||
if len(lst) == 0:
|
||||
return 0
|
||||
|
||||
'''
|
||||
Recursive step progresses toward the simple case
|
||||
'''
|
||||
return lst[0] + sum_list(lst[1:])
|
||||
|
||||
|
||||
def test_sum_list():
|
||||
assert sum_list([]) == 0
|
||||
assert sum_list([0]) == 0
|
||||
assert sum_list([1, 2, 6]) == 9
|
||||
assert sum_list([-1, 4, -100, 50]) == -47
|
||||
assert sum_list([1, 2, 3, 4, 5, 6]) == 21
|
||||
|
||||
|
||||
test_sum_list()
|
||||
|
||||
'''
|
||||
3. Cumpute the n-th term of the Fiboanacci sequence:
|
||||
'''
|
||||
|
||||
|
||||
def fibo(n):
|
||||
"""
|
||||
Computes the n-th term of the Fibonacci sequence
|
||||
input:
|
||||
n - the index of the desired term
|
||||
output:
|
||||
The value of the desired term
|
||||
"""
|
||||
|
||||
'''
|
||||
This is the best case, no recursion
|
||||
'''
|
||||
if n == 0 or n == 1:
|
||||
return 1
|
||||
|
||||
'''
|
||||
Recursive step progresses toward the simple case
|
||||
'''
|
||||
return fibo(n - 2) + fibo(n - 1)
|
||||
|
||||
|
||||
def test_fibo():
|
||||
fib = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
|
||||
|
||||
for index in range(0, len(fib)):
|
||||
assert fibo(index) == fib[index]
|
||||
|
||||
|
||||
test_fibo()
|
||||
|
||||
'''
|
||||
4. Determine whether a given string is a palindrome
|
||||
'''
|
||||
|
||||
|
||||
def palindrome(s):
|
||||
"""
|
||||
Determine if the given string is a palindrome
|
||||
input:
|
||||
s - the string
|
||||
output:
|
||||
True if s is palindrome, False otherwise
|
||||
"""
|
||||
|
||||
'''
|
||||
This is the best case, no recursion
|
||||
'''
|
||||
if len(s) < 2:
|
||||
return True
|
||||
|
||||
'''
|
||||
Recursive step progresses toward the simple case
|
||||
'''
|
||||
return s[0] == s[-1] and palindrome(s[1:-1])
|
||||
|
||||
|
||||
def test_palindrome():
|
||||
assert palindrome("") is True
|
||||
assert palindrome("a") is True
|
||||
assert palindrome("axa") is True
|
||||
assert palindrome("axdf") is False
|
||||
assert palindrome("axdfdxa") is True
|
||||
assert palindrome("abcddcba") is True
|
||||
assert palindrome("abcddca") is False
|
||||
|
||||
|
||||
test_palindrome()
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
"""
|
||||
Created on Dec 6, 2016
|
||||
|
||||
@author: Arthur
|
||||
"""
|
||||
import timeit
|
||||
from texttable import Texttable
|
||||
|
||||
'''
|
||||
1. Here we have two implementation for the Fibonacci sequence
|
||||
'''
|
||||
|
||||
|
||||
def fibonacci_iterative(n):
|
||||
"""
|
||||
Iterative implementation for computing n-th term of the Fibonacci sequence
|
||||
"""
|
||||
if n == 0:
|
||||
return 0
|
||||
x = 0
|
||||
y = 1
|
||||
for i in range(1, n):
|
||||
z = x + y
|
||||
x = y
|
||||
y = z
|
||||
return y
|
||||
|
||||
|
||||
def fibonacci_recursive(n):
|
||||
"""
|
||||
Recursive implementation for computing n-th term of the Fibonacci sequence
|
||||
"""
|
||||
if n < 2:
|
||||
return n
|
||||
return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2)
|
||||
|
||||
|
||||
'''
|
||||
2. We test them to see they work correctly
|
||||
'''
|
||||
|
||||
|
||||
def test_fibonacci():
|
||||
fib = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
|
||||
for i in range(0, len(fib)):
|
||||
assert fibonacci_iterative(i) == fib[i], (i, fibonacci_iterative(i))
|
||||
assert fibonacci_recursive(i) == fib[i]
|
||||
|
||||
|
||||
test_fibonacci()
|
||||
|
||||
'''
|
||||
NB!
|
||||
To run the function below, you must have installed the texttable component from:
|
||||
https://github.com/foutaise/texttable
|
||||
'''
|
||||
|
||||
|
||||
def build_result_table():
|
||||
table = Texttable()
|
||||
table.add_row(['Term', 'Iterative', 'Recursive'])
|
||||
for term in [10, 20, 30, 32, 34, 36]:
|
||||
# Iterative
|
||||
start_iter = timeit.default_timer()
|
||||
row = fibonacci_iterative(term)
|
||||
end_iter = timeit.default_timer()
|
||||
# Recursive
|
||||
start_rec = timeit.default_timer()
|
||||
row = fibonacci_recursive(term)
|
||||
end_rec = timeit.default_timer()
|
||||
|
||||
table.add_row([term, end_iter - start_iter, end_rec - start_rec])
|
||||
return table
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(build_result_table().draw())
|
||||
|
||||
'''
|
||||
In case you cannot run the example, this is what it is supposed to look like:
|
||||
|
||||
+------+-----------+-----------+
|
||||
| Term | Iterative | Recursive |
|
||||
+------+-----------+-----------+
|
||||
| 10 | 0 | 0 |
|
||||
+------+-----------+-----------+
|
||||
| 20 | 0 | 3 |
|
||||
+------+-----------+-----------+
|
||||
| 30 | 0 | 357 |
|
||||
+------+-----------+-----------+
|
||||
| 32 | 0 | 937 |
|
||||
+------+-----------+-----------+
|
||||
| 34 | 0 | 2440 |
|
||||
+------+-----------+-----------+
|
||||
| 36 | 0 | 6437 |
|
||||
+------+-----------+-----------+
|
||||
|
||||
NB!
|
||||
0 milliseconds is not really 0, it's just too short to measure accurately
|
||||
'''
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
"""
|
||||
Created on Dec 6, 2016
|
||||
|
||||
@author: Arthur
|
||||
"""
|
||||
|
||||
from texttable import Texttable
|
||||
import timeit
|
||||
|
||||
from lecture.examples.ex05_complexity import fibonacci_recursive, fibonacci_iterative
|
||||
|
||||
'''
|
||||
4. To speed up the recursive implementation, we use memoization to store interim results
|
||||
'''
|
||||
results = {0: 0, 1: 1}
|
||||
|
||||
|
||||
def fibonacci_memoization(n):
|
||||
if n not in results:
|
||||
results[n] = fibonacci_memoization(n - 1) + fibonacci_memoization(n - 2)
|
||||
return results[n]
|
||||
|
||||
|
||||
dataList = []
|
||||
|
||||
'''
|
||||
NB!
|
||||
To run the function below, you must have installed the texttable component from:
|
||||
https://github.com/foutaise/texttable
|
||||
'''
|
||||
|
||||
|
||||
def build_result_table():
|
||||
table = Texttable()
|
||||
table.add_row(['Term', 'Iterative', 'Recursive', 'Memoization'])
|
||||
for term in [10, 20, 30, 32, 34, 36]:
|
||||
# Iterative
|
||||
start_iter = timeit.default_timer()
|
||||
row = fibonacci_iterative(term)
|
||||
end_iter = timeit.default_timer()
|
||||
# Recursive
|
||||
start_rec = timeit.default_timer()
|
||||
row = fibonacci_recursive(term)
|
||||
end_rec = timeit.default_timer()
|
||||
# Recursive with memoization
|
||||
start_mem = timeit.default_timer()
|
||||
row = fibonacci_memoization(term)
|
||||
end_mem = timeit.default_timer()
|
||||
|
||||
table.add_row([term, end_iter - start_iter, end_rec - start_rec, end_mem - start_mem])
|
||||
return table
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(build_result_table().draw())
|
||||
|
||||
'''
|
||||
In case you cannot run the example, this is what it is supposed to look like:
|
||||
|
||||
+------+-----------+-----------+-------------+
|
||||
| Term | Iterative | Recursive | Memoization |
|
||||
+------+-----------+-----------+-------------+
|
||||
| 10 | 0 | 0 | 0 |
|
||||
+------+-----------+-----------+-------------+
|
||||
| 20 | 0 | 3 | 0 |
|
||||
+------+-----------+-----------+-------------+
|
||||
| 30 | 0 | 345 | 0 |
|
||||
+------+-----------+-----------+-------------+
|
||||
| 32 | 0 | 912 | 0 |
|
||||
+------+-----------+-----------+-------------+
|
||||
| 34 | 0 | 2381 | 0 |
|
||||
+------+-----------+-----------+-------------+
|
||||
| 36 | 0 | 6215 | 0 |
|
||||
+------+-----------+-----------+-------------+
|
||||
|
||||
NB!
|
||||
0 milliseconds is not really 0, it's just too short to measure accurately
|
||||
'''
|
||||
@@ -0,0 +1,99 @@
|
||||
"""
|
||||
Created on Dec 7, 2016
|
||||
|
||||
@author: Arthur
|
||||
"""
|
||||
import timeit
|
||||
from texttable import Texttable
|
||||
|
||||
|
||||
def hanoi(n, x, y, z):
|
||||
"""
|
||||
n - number of disks on the x stick
|
||||
x - source Stick
|
||||
y - destination stick
|
||||
z - intermediate stick
|
||||
"""
|
||||
if n == 1:
|
||||
return
|
||||
hanoi(n - 1, x, z, y)
|
||||
hanoi(n - 1, z, y, x)
|
||||
|
||||
|
||||
def hanoi_verbose(n, x, y, z):
|
||||
"""
|
||||
n - number of disks on the x stick
|
||||
x - source Stick
|
||||
y - destination stick
|
||||
z - intermediate stick
|
||||
"""
|
||||
if n == 1:
|
||||
print("Disk 1 from ", x, " to ", y)
|
||||
return
|
||||
hanoi(n - 1, x, z, y)
|
||||
print("Disk ", n, " from ", x, " to ", y)
|
||||
hanoi(n - 1, z, y, x)
|
||||
|
||||
|
||||
'''
|
||||
NB!
|
||||
To run the function below, you must have installed the texttable component from:
|
||||
https://github.com/foutaise/texttable
|
||||
'''
|
||||
|
||||
|
||||
def build_result_table():
|
||||
table = Texttable()
|
||||
table.add_row(['disks', 'seconds'])
|
||||
for term in range(10, 26):
|
||||
t1 = timeit.default_timer()
|
||||
hanoi(term, "X", "Y", "Z")
|
||||
t2 = timeit.default_timer()
|
||||
table.add_row([term, t2 - t1])
|
||||
return table
|
||||
|
||||
|
||||
print(build_result_table().draw())
|
||||
|
||||
'''
|
||||
In case you cannot run the example, this is what it is supposed to look like:
|
||||
|
||||
+-------+-------------+
|
||||
| Disks | Miliseconds |
|
||||
+-------+-------------+
|
||||
| 10 | 0 |
|
||||
+-------+-------------+
|
||||
| 11 | 0 |
|
||||
+-------+-------------+
|
||||
| 12 | 1 |
|
||||
+-------+-------------+
|
||||
| 13 | 1 |
|
||||
+-------+-------------+
|
||||
| 14 | 3 |
|
||||
+-------+-------------+
|
||||
| 15 | 5 |
|
||||
+-------+-------------+
|
||||
| 16 | 10 |
|
||||
+-------+-------------+
|
||||
| 17 | 19 |
|
||||
+-------+-------------+
|
||||
| 18 | 39 |
|
||||
+-------+-------------+
|
||||
| 19 | 76 |
|
||||
+-------+-------------+
|
||||
| 20 | 154 |
|
||||
+-------+-------------+
|
||||
| 21 | 312 |
|
||||
+-------+-------------+
|
||||
| 22 | 614 |
|
||||
+-------+-------------+
|
||||
| 23 | 1223 |
|
||||
+-------+-------------+
|
||||
| 24 | 2440 |
|
||||
+-------+-------------+
|
||||
| 25 | 4891 |
|
||||
+-------+-------------+
|
||||
|
||||
NB!
|
||||
0 miliseconds is not really 0, it's just too short to measure accurately
|
||||
'''
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
"""
|
||||
Examples for sequential searching
|
||||
"""
|
||||
import random
|
||||
import timeit
|
||||
from texttable import Texttable
|
||||
|
||||
|
||||
def search_iter(data: list, key):
|
||||
for i in range(len(data)):
|
||||
if data[i] == key:
|
||||
return i
|
||||
return -1
|
||||
|
||||
|
||||
def search_rec(data: list, key, pos: int = 0):
|
||||
if 0 > pos or pos >= len(data):
|
||||
return -1
|
||||
if data[pos] == key:
|
||||
return key
|
||||
return search_rec(key, data, pos + 1)
|
||||
|
||||
|
||||
def generate_list(length: int):
|
||||
"""
|
||||
Generate a list of given length with elements [0, ... , n-1]
|
||||
:return: The newly generated list
|
||||
"""
|
||||
return list(range(length))
|
||||
|
||||
|
||||
'''
|
||||
NB!
|
||||
To run the function below, you must have installed the texttable component from:
|
||||
https://github.com/foutaise/texttable
|
||||
'''
|
||||
|
||||
|
||||
def build_result_table(algorithms: list, list_lengths: list):
|
||||
table = Texttable()
|
||||
table.add_row(['algorithm'] + list_lengths)
|
||||
|
||||
for algorithm in algorithms:
|
||||
table_row = [algorithm.__name__]
|
||||
for list_length in list_lengths:
|
||||
data = generate_list(list_length)
|
||||
t1 = timeit.default_timer()
|
||||
# -1 is not in the list, so worst case complexity
|
||||
algorithm(data, -1)
|
||||
t2 = timeit.default_timer()
|
||||
table_row.append(t2 - t1)
|
||||
table.add_row(table_row)
|
||||
return table
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
list_lengths = [1_000_000, 2_000_000, 4_000_000, 8_000_000, 16_000_000]
|
||||
# Adding search_rec here will crash with recursion depth exceeded error
|
||||
# TODO How do we add the binary search implementations here?
|
||||
algorithms = [search_iter]
|
||||
print(build_result_table(algorithms, list_lengths).draw())
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
def binary_search_rec(data: list, key):
|
||||
"""
|
||||
Binary search, recursive implementation
|
||||
:param data: List in which search is performed in
|
||||
:param key: Search key
|
||||
:return: Position of element, -1 if element was not found
|
||||
"""
|
||||
return _binary_search_impl(data, key, 0, len(data) - 1)
|
||||
|
||||
|
||||
def _binary_search_impl(data: list, key, left: int, right: int):
|
||||
"""
|
||||
This is an implementation method. _ means that the method should not be called from other modules.
|
||||
"""
|
||||
if right < left:
|
||||
return -1
|
||||
m = (left + right) // 2
|
||||
if data[m] > key:
|
||||
return _binary_search_impl(data, key, left, m - 1)
|
||||
if data[m] < key:
|
||||
return _binary_search_impl(data, key, m + 1, right)
|
||||
if data[m] == key:
|
||||
return m
|
||||
|
||||
|
||||
def binary_search_iter(data: list, key):
|
||||
left = 0
|
||||
right = len(data) - 1
|
||||
|
||||
while left <= right:
|
||||
middle = (left + right) // 2
|
||||
if data[middle] > key:
|
||||
right = middle - 1
|
||||
if data[middle] < key:
|
||||
left = middle + 1
|
||||
if data[middle] == key:
|
||||
return middle
|
||||
return -1
|
||||
|
||||
|
||||
# TODO Take a look at this method
|
||||
def test_binary_search():
|
||||
binary_search_alg = [binary_search_iter, binary_search_rec]
|
||||
|
||||
for bs_alg in binary_search_alg:
|
||||
data = list(range(1000))
|
||||
for i in range(0, 1000):
|
||||
assert i == bs_alg(data, i)
|
||||
assert -1 == bs_alg(list(range(100)), 101)
|
||||
assert -1 == bs_alg(list(range(100)), -1)
|
||||
|
||||
|
||||
test_binary_search()
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
from random import choice
|
||||
|
||||
|
||||
def create_person(name: str, age: int):
|
||||
return {"name": name, "age": age}
|
||||
|
||||
|
||||
def get_name(person: dict):
|
||||
return person["name"]
|
||||
|
||||
|
||||
def get_age(person: dict):
|
||||
return person["age"]
|
||||
|
||||
|
||||
def generate():
|
||||
"""
|
||||
Generate some persons
|
||||
"""
|
||||
result = []
|
||||
|
||||
family_name = ['Popescu', 'Marian', 'Pop', 'Lazarescu', 'Dincu']
|
||||
given_name = ['Anca', 'Emilia', 'Liviu', 'Marius']
|
||||
age = [17, 18, 19, 20]
|
||||
|
||||
for i in range(20):
|
||||
result.append(create_person(choice(family_name) + " " + choice(given_name), choice(age)))
|
||||
return result
|
||||
|
||||
|
||||
'''
|
||||
1. Generate people
|
||||
'''
|
||||
result = generate()
|
||||
|
||||
'''
|
||||
2. First we sort the list by name (ascending)
|
||||
'''
|
||||
result.sort(key=lambda person: person["name"])
|
||||
|
||||
'''
|
||||
3. Then we sort by age (descending) - the sorts are STABLE
|
||||
'''
|
||||
result.sort(key=lambda person: person["age"], reverse=True)
|
||||
|
||||
'''
|
||||
4. People of the same age are ordered by name
|
||||
'''
|
||||
for p in result:
|
||||
print(p)
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
"""
|
||||
Insertion sort. An O(n^2) complexity algorithm
|
||||
"""
|
||||
|
||||
|
||||
def insertion_sort(data: list):
|
||||
for i in range(1, len(data)):
|
||||
val = data[i]
|
||||
j = i - 1
|
||||
while (j >= 0) and (data[j] > val):
|
||||
data[j + 1] = data[j]
|
||||
j = j - 1
|
||||
data[j + 1] = val
|
||||
return data
|
||||
|
||||
|
||||
"""
|
||||
Binary insertion sort
|
||||
Source: https://www.geeksforgeeks.org/binary-insertion-sort/ (code contributed by Mohit Gupta_OMG)
|
||||
"""
|
||||
|
||||
|
||||
def binary_search(arr, val, start, end):
|
||||
# we need to distinguish whether we should insert before or after the left boundary. imagine [0] is the last
|
||||
# step of the binary search and we need to decide where to insert -1
|
||||
if start == end:
|
||||
if arr[start] > val:
|
||||
return start
|
||||
else:
|
||||
return start + 1
|
||||
|
||||
# this occurs if we are moving beyond left's boundary meaning the left boundary is the least position to find a
|
||||
# number greater than val
|
||||
if start > end:
|
||||
return start
|
||||
|
||||
mid = (start + end) // 2
|
||||
if arr[mid] < val:
|
||||
return binary_search(arr, val, mid + 1, end)
|
||||
elif arr[mid] > val:
|
||||
return binary_search(arr, val, start, mid - 1)
|
||||
else:
|
||||
return mid
|
||||
|
||||
|
||||
def binary_insertion_sort(data: list):
|
||||
for i in range(1, len(data)):
|
||||
val = data[i]
|
||||
j = binary_search(data, val, 0, i - 1)
|
||||
# This is O(n) space complexity, but it can be simplified by moving elements one by one
|
||||
data = data[:j] + [val] + data[j:i] + data[i + 1:]
|
||||
return data
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
"""
|
||||
Merge Sort implementation
|
||||
"""
|
||||
|
||||
|
||||
def merge_sort(array):
|
||||
if len(array) < 2:
|
||||
return array
|
||||
|
||||
mid = len(array) // 2
|
||||
left_half = array[:mid]
|
||||
right_half = array[mid:]
|
||||
merge_sort(left_half)
|
||||
merge_sort(right_half)
|
||||
merge(left_half, right_half, array)
|
||||
|
||||
|
||||
def merge(l1, l2, lrez):
|
||||
i = 0
|
||||
j = 0
|
||||
l = []
|
||||
while i < len(l1) and j < len(l2):
|
||||
if l1[i] < l2[j]:
|
||||
l.append(l1[i])
|
||||
i = i + 1
|
||||
else:
|
||||
l.append(l2[j])
|
||||
j = j + 1
|
||||
while i < len(l1):
|
||||
l.append(l1[i])
|
||||
i = i + 1
|
||||
while j < len(l2):
|
||||
l.append(l2[j])
|
||||
j = j + 1
|
||||
lrez.clear()
|
||||
lrez.extend(l)
|
||||
+649
@@ -0,0 +1,649 @@
|
||||
"""
|
||||
TimSort implementation from https://github.com/reingart/pypy/blob/master/rpython/rlib/listsort.py
|
||||
"""
|
||||
|
||||
"""
|
||||
NB! Overflow checks removed so code is no longer production ready!
|
||||
"""
|
||||
|
||||
|
||||
# from rpython.rlib.rarithmetic import ovfcheck
|
||||
|
||||
|
||||
## ------------------------------------------------------------------------
|
||||
## Lots of code for an adaptive, stable, natural mergesort. There are many
|
||||
## pieces to this algorithm; read listsort.txt for overviews and details.
|
||||
## ------------------------------------------------------------------------
|
||||
## Adapted from CPython, original code and algorithms by Tim Peters
|
||||
|
||||
def make_timsort_class(getitem=None, setitem=None, length=None,
|
||||
getitem_slice=None, lt=None):
|
||||
if getitem is None:
|
||||
def getitem(list, item):
|
||||
return list[item]
|
||||
|
||||
if setitem is None:
|
||||
def setitem(list, item, value):
|
||||
list[item] = value
|
||||
|
||||
if length is None:
|
||||
def length(list):
|
||||
return len(list)
|
||||
|
||||
if getitem_slice is None:
|
||||
def getitem_slice(list, start, stop):
|
||||
return list[start:stop]
|
||||
|
||||
if lt is None:
|
||||
def lt(a, b):
|
||||
return a < b
|
||||
|
||||
class TimSort(object):
|
||||
"""TimSort(list).sort()
|
||||
|
||||
Sorts the list in-place, using the overridable method lt() for comparison.
|
||||
"""
|
||||
|
||||
def __init__(self, list, listlength=None):
|
||||
self.list = list
|
||||
if listlength is None:
|
||||
listlength = length(list)
|
||||
self.listlength = listlength
|
||||
|
||||
def setitem(self, item, val):
|
||||
setitem(self.list, item, val)
|
||||
|
||||
def lt(self, a, b):
|
||||
return lt(a, b)
|
||||
|
||||
def le(self, a, b):
|
||||
return not self.lt(b, a) # always use self.lt() as the primitive
|
||||
|
||||
# binarysort is the best method for sorting small arrays: it does
|
||||
# few compares, but can do data movement quadratic in the number of
|
||||
# elements.
|
||||
# "a" is a contiguous slice of a list, and is sorted via binary insertion.
|
||||
# This sort is stable.
|
||||
# On entry, the first "sorted" elements are already sorted.
|
||||
# Even in case of error, the output slice will be some permutation of
|
||||
# the input (nothing is lost or duplicated).
|
||||
|
||||
def binarysort(self, a, sorted=1):
|
||||
for start in range(a.base + sorted, a.base + a.len):
|
||||
# set l to where list[start] belongs
|
||||
l = a.base
|
||||
r = start
|
||||
pivot = a.getitem(r)
|
||||
# Invariants:
|
||||
# pivot >= all in [base, l).
|
||||
# pivot < all in [r, start).
|
||||
# The second is vacuously true at the start.
|
||||
while l < r:
|
||||
p = l + ((r - l) >> 1)
|
||||
if self.lt(pivot, a.getitem(p)):
|
||||
r = p
|
||||
else:
|
||||
l = p + 1
|
||||
assert l == r
|
||||
# The invariants still hold, so pivot >= all in [base, l) and
|
||||
# pivot < all in [l, start), so pivot belongs at l. Note
|
||||
# that if there are elements equal to pivot, l points to the
|
||||
# first slot after them -- that's why this sort is stable.
|
||||
# Slide over to make room.
|
||||
for p in range(start, l, -1):
|
||||
a.setitem(p, a.getitem(p - 1))
|
||||
a.setitem(l, pivot)
|
||||
|
||||
# Compute the length of the run in the slice "a".
|
||||
# "A run" is the longest ascending sequence, with
|
||||
#
|
||||
# a[0] <= a[1] <= a[2] <= ...
|
||||
#
|
||||
# or the longest descending sequence, with
|
||||
#
|
||||
# a[0] > a[1] > a[2] > ...
|
||||
#
|
||||
# Return (run, descending) where descending is False in the former case,
|
||||
# or True in the latter.
|
||||
# For its intended use in a stable mergesort, the strictness of the defn of
|
||||
# "descending" is needed so that the caller can safely reverse a descending
|
||||
# sequence without violating stability (strict > ensures there are no equal
|
||||
# elements to get out of order).
|
||||
|
||||
def count_run(self, a):
|
||||
if a.len <= 1:
|
||||
n = a.len
|
||||
descending = False
|
||||
else:
|
||||
n = 2
|
||||
if self.lt(a.getitem(a.base + 1), a.getitem(a.base)):
|
||||
descending = True
|
||||
for p in range(a.base + 2, a.base + a.len):
|
||||
if self.lt(a.getitem(p), a.getitem(p - 1)):
|
||||
n += 1
|
||||
else:
|
||||
break
|
||||
else:
|
||||
descending = False
|
||||
for p in range(a.base + 2, a.base + a.len):
|
||||
if self.lt(a.getitem(p), a.getitem(p - 1)):
|
||||
break
|
||||
else:
|
||||
n += 1
|
||||
return ListSlice(a.list, a.base, n), descending
|
||||
|
||||
# Locate the proper position of key in a sorted vector; if the vector
|
||||
# contains an element equal to key, return the position immediately to the
|
||||
# left of the leftmost equal element -- or to the right of the rightmost
|
||||
# equal element if the flag "rightmost" is set.
|
||||
#
|
||||
# "hint" is an index at which to begin the search, 0 <= hint < a.len.
|
||||
# The closer hint is to the final result, the faster this runs.
|
||||
#
|
||||
# The return value is the index 0 <= k <= a.len such that
|
||||
#
|
||||
# a[k-1] < key <= a[k] (if rightmost is False)
|
||||
# a[k-1] <= key < a[k] (if rightmost is True)
|
||||
#
|
||||
# as long as the indices are in bound. IOW, key belongs at index k;
|
||||
# or, IOW, the first k elements of a should precede key, and the last
|
||||
# n-k should follow key.
|
||||
|
||||
def gallop(self, key, a, hint, rightmost):
|
||||
assert 0 <= hint < a.len
|
||||
if rightmost:
|
||||
lower = self.le # search for the largest k for which a[k] <= key
|
||||
else:
|
||||
lower = self.lt # search for the largest k for which a[k] < key
|
||||
|
||||
p = a.base + hint
|
||||
lastofs = 0
|
||||
ofs = 1
|
||||
if lower(a.getitem(p), key):
|
||||
# a[hint] < key -- gallop right, until
|
||||
# a[hint + lastofs] < key <= a[hint + ofs]
|
||||
|
||||
maxofs = a.len - hint # a[a.len-1] is highest
|
||||
while ofs < maxofs:
|
||||
if lower(a.getitem(p + ofs), key):
|
||||
lastofs = ofs
|
||||
try:
|
||||
pass
|
||||
# ofs = ovfcheck(ofs << 1)
|
||||
except OverflowError:
|
||||
ofs = maxofs
|
||||
else:
|
||||
ofs = ofs + 1
|
||||
else: # key <= a[hint + ofs]
|
||||
break
|
||||
|
||||
if ofs > maxofs:
|
||||
ofs = maxofs
|
||||
# Translate back to offsets relative to a.
|
||||
lastofs += hint
|
||||
ofs += hint
|
||||
|
||||
else:
|
||||
# key <= a[hint] -- gallop left, until
|
||||
# a[hint - ofs] < key <= a[hint - lastofs]
|
||||
maxofs = hint + 1 # a[0] is lowest
|
||||
while ofs < maxofs:
|
||||
if lower(a.getitem(p - ofs), key):
|
||||
break
|
||||
else:
|
||||
# key <= a[hint - ofs]
|
||||
lastofs = ofs
|
||||
try:
|
||||
pass
|
||||
# ofs = ovfcheck(ofs << 1)
|
||||
except OverflowError:
|
||||
ofs = maxofs
|
||||
else:
|
||||
ofs = ofs + 1
|
||||
if ofs > maxofs:
|
||||
ofs = maxofs
|
||||
# Translate back to positive offsets relative to a.
|
||||
lastofs, ofs = hint - ofs, hint - lastofs
|
||||
|
||||
assert -1 <= lastofs < ofs <= a.len
|
||||
|
||||
# Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
|
||||
# right of lastofs but no farther right than ofs. Do a binary
|
||||
# search, with invariant a[lastofs-1] < key <= a[ofs].
|
||||
|
||||
lastofs += 1
|
||||
while lastofs < ofs:
|
||||
m = lastofs + ((ofs - lastofs) >> 1)
|
||||
if lower(a.getitem(a.base + m), key):
|
||||
lastofs = m + 1 # a[m] < key
|
||||
else:
|
||||
ofs = m # key <= a[m]
|
||||
|
||||
assert lastofs == ofs # so a[ofs-1] < key <= a[ofs]
|
||||
return ofs
|
||||
|
||||
# hint for the annotator: the argument 'rightmost' is always passed in as
|
||||
# a constant (either True or False), so we can specialize the function for
|
||||
# the two cases. (This is actually needed for technical reasons: the
|
||||
# variable 'lower' must contain a known method, which is the case in each
|
||||
# specialized version but not in the unspecialized one.)
|
||||
gallop._annspecialcase_ = "specialize:arg(4)"
|
||||
|
||||
# ____________________________________________________________
|
||||
|
||||
# When we get into galloping mode, we stay there until both runs win less
|
||||
# often than MIN_GALLOP consecutive times. See listsort.txt for more info.
|
||||
MIN_GALLOP = 7
|
||||
|
||||
def merge_init(self):
|
||||
# This controls when we get *into* galloping mode. It's initialized
|
||||
# to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
|
||||
# random data, and lower for highly structured data.
|
||||
self.min_gallop = self.MIN_GALLOP
|
||||
|
||||
# A stack of n pending runs yet to be merged. Run #i starts at
|
||||
# address pending[i].base and extends for pending[i].len elements.
|
||||
# It's always true (so long as the indices are in bounds) that
|
||||
#
|
||||
# pending[i].base + pending[i].len == pending[i+1].base
|
||||
#
|
||||
# so we could cut the storage for this, but it's a minor amount,
|
||||
# and keeping all the info explicit simplifies the code.
|
||||
self.pending = []
|
||||
|
||||
# Merge the slice "a" with the slice "b" in a stable way, in-place.
|
||||
# a.len and b.len must be > 0, and a.base + a.len == b.base.
|
||||
# Must also have that b.list[b.base] < a.list[a.base], that
|
||||
# a.list[a.base+a.len-1] belongs at the end of the merge, and should have
|
||||
# a.len <= b.len. See listsort.txt for more info.
|
||||
|
||||
def merge_lo(self, a, b):
|
||||
assert a.len > 0 and b.len > 0 and a.base + a.len == b.base
|
||||
min_gallop = self.min_gallop
|
||||
dest = a.base
|
||||
a = a.copyitems()
|
||||
|
||||
# Invariant: elements in "a" are waiting to be reinserted into the list
|
||||
# at "dest". They should be merged with the elements of "b".
|
||||
# b.base == dest + a.len.
|
||||
# We use a finally block to ensure that the elements remaining in
|
||||
# the copy "a" are reinserted back into self.list in all cases.
|
||||
try:
|
||||
self.setitem(dest, b.popleft())
|
||||
dest += 1
|
||||
if a.len == 1 or b.len == 0:
|
||||
return
|
||||
|
||||
while True:
|
||||
acount = 0 # number of times A won in a row
|
||||
bcount = 0 # number of times B won in a row
|
||||
|
||||
# Do the straightforward thing until (if ever) one run
|
||||
# appears to win consistently.
|
||||
while True:
|
||||
if self.lt(b.getitem(b.base), a.getitem(a.base)):
|
||||
self.setitem(dest, b.popleft())
|
||||
dest += 1
|
||||
if b.len == 0:
|
||||
return
|
||||
bcount += 1
|
||||
acount = 0
|
||||
if bcount >= min_gallop:
|
||||
break
|
||||
else:
|
||||
self.setitem(dest, a.popleft())
|
||||
dest += 1
|
||||
if a.len == 1:
|
||||
return
|
||||
acount += 1
|
||||
bcount = 0
|
||||
if acount >= min_gallop:
|
||||
break
|
||||
|
||||
# One run is winning so consistently that galloping may
|
||||
# be a huge win. So try that, and continue galloping until
|
||||
# (if ever) neither run appears to be winning consistently
|
||||
# anymore.
|
||||
min_gallop += 1
|
||||
|
||||
while True:
|
||||
min_gallop -= min_gallop > 1
|
||||
self.min_gallop = min_gallop
|
||||
|
||||
acount = self.gallop(b.getitem(b.base), a, hint=0,
|
||||
rightmost=True)
|
||||
for p in range(a.base, a.base + acount):
|
||||
self.setitem(dest, a.getitem(p))
|
||||
dest += 1
|
||||
a.advance(acount)
|
||||
# a.len==0 is impossible now if the comparison
|
||||
# function is consistent, but we can't assume
|
||||
# that it is.
|
||||
if a.len <= 1:
|
||||
return
|
||||
|
||||
self.setitem(dest, b.popleft())
|
||||
dest += 1
|
||||
if b.len == 0:
|
||||
return
|
||||
|
||||
bcount = self.gallop(a.getitem(a.base), b, hint=0,
|
||||
rightmost=False)
|
||||
for p in range(b.base, b.base + bcount):
|
||||
self.setitem(dest, b.getitem(p))
|
||||
dest += 1
|
||||
b.advance(bcount)
|
||||
if b.len == 0:
|
||||
return
|
||||
|
||||
self.setitem(dest, a.popleft())
|
||||
dest += 1
|
||||
if a.len == 1:
|
||||
return
|
||||
|
||||
if acount < self.MIN_GALLOP and bcount < self.MIN_GALLOP:
|
||||
break
|
||||
|
||||
min_gallop += 1 # penalize it for leaving galloping mode
|
||||
self.min_gallop = min_gallop
|
||||
|
||||
finally:
|
||||
# The last element of a belongs at the end of the merge, so we copy
|
||||
# the remaining elements of b before the remaining elements of a.
|
||||
assert a.len >= 0 and b.len >= 0
|
||||
for p in range(b.base, b.base + b.len):
|
||||
self.setitem(dest, b.getitem(p))
|
||||
dest += 1
|
||||
for p in range(a.base, a.base + a.len):
|
||||
self.setitem(dest, a.getitem(p))
|
||||
dest += 1
|
||||
|
||||
# Same as merge_lo(), but should have a.len >= b.len.
|
||||
|
||||
def merge_hi(self, a, b):
|
||||
assert a.len > 0 and b.len > 0 and a.base + a.len == b.base
|
||||
min_gallop = self.min_gallop
|
||||
dest = b.base + b.len
|
||||
b = b.copyitems()
|
||||
|
||||
# Invariant: elements in "b" are waiting to be reinserted into the list
|
||||
# before "dest". They should be merged with the elements of "a".
|
||||
# a.base + a.len == dest - b.len.
|
||||
# We use a finally block to ensure that the elements remaining in
|
||||
# the copy "b" are reinserted back into self.list in all cases.
|
||||
try:
|
||||
dest -= 1
|
||||
self.setitem(dest, a.popright())
|
||||
if a.len == 0 or b.len == 1:
|
||||
return
|
||||
|
||||
while True:
|
||||
acount = 0 # number of times A won in a row
|
||||
bcount = 0 # number of times B won in a row
|
||||
|
||||
# Do the straightforward thing until (if ever) one run
|
||||
# appears to win consistently.
|
||||
while True:
|
||||
nexta = a.getitem(a.base + a.len - 1)
|
||||
nextb = b.getitem(b.base + b.len - 1)
|
||||
if self.lt(nextb, nexta):
|
||||
dest -= 1
|
||||
self.setitem(dest, nexta)
|
||||
a.len -= 1
|
||||
if a.len == 0:
|
||||
return
|
||||
acount += 1
|
||||
bcount = 0
|
||||
if acount >= min_gallop:
|
||||
break
|
||||
else:
|
||||
dest -= 1
|
||||
self.setitem(dest, nextb)
|
||||
b.len -= 1
|
||||
if b.len == 1:
|
||||
return
|
||||
bcount += 1
|
||||
acount = 0
|
||||
if bcount >= min_gallop:
|
||||
break
|
||||
|
||||
# One run is winning so consistently that galloping may
|
||||
# be a huge win. So try that, and continue galloping until
|
||||
# (if ever) neither run appears to be winning consistently
|
||||
# anymore.
|
||||
min_gallop += 1
|
||||
|
||||
while True:
|
||||
min_gallop -= min_gallop > 1
|
||||
self.min_gallop = min_gallop
|
||||
|
||||
nextb = b.getitem(b.base + b.len - 1)
|
||||
k = self.gallop(nextb, a, hint=a.len - 1, rightmost=True)
|
||||
acount = a.len - k
|
||||
for p in range(a.base + a.len - 1, a.base + k - 1, -1):
|
||||
dest -= 1
|
||||
self.setitem(dest, a.getitem(p))
|
||||
a.len -= acount
|
||||
if a.len == 0:
|
||||
return
|
||||
|
||||
dest -= 1
|
||||
self.setitem(dest, b.popright())
|
||||
if b.len == 1:
|
||||
return
|
||||
|
||||
nexta = a.getitem(a.base + a.len - 1)
|
||||
k = self.gallop(nexta, b, hint=b.len - 1, rightmost=False)
|
||||
bcount = b.len - k
|
||||
for p in range(b.base + b.len - 1, b.base + k - 1, -1):
|
||||
dest -= 1
|
||||
self.setitem(dest, b.getitem(p))
|
||||
b.len -= bcount
|
||||
# b.len==0 is impossible now if the comparison
|
||||
# function is consistent, but we can't assume
|
||||
# that it is.
|
||||
if b.len <= 1:
|
||||
return
|
||||
|
||||
dest -= 1
|
||||
self.setitem(dest, a.popright())
|
||||
if a.len == 0:
|
||||
return
|
||||
|
||||
if acount < self.MIN_GALLOP and bcount < self.MIN_GALLOP:
|
||||
break
|
||||
|
||||
min_gallop += 1 # penalize it for leaving galloping mode
|
||||
self.min_gallop = min_gallop
|
||||
|
||||
finally:
|
||||
# The last element of a belongs at the end of the merge, so we copy
|
||||
# the remaining elements of a and then the remaining elements of b.
|
||||
assert a.len >= 0 and b.len >= 0
|
||||
for p in range(a.base + a.len - 1, a.base - 1, -1):
|
||||
dest -= 1
|
||||
self.setitem(dest, a.getitem(p))
|
||||
for p in range(b.base + b.len - 1, b.base - 1, -1):
|
||||
dest -= 1
|
||||
self.setitem(dest, b.getitem(p))
|
||||
|
||||
# Merge the two runs at stack indices i and i+1.
|
||||
|
||||
def merge_at(self, i):
|
||||
a = self.pending[i]
|
||||
b = self.pending[i + 1]
|
||||
assert a.len > 0 and b.len > 0
|
||||
assert a.base + a.len == b.base
|
||||
|
||||
# Record the length of the combined runs and remove the run b
|
||||
self.pending[i] = ListSlice(self.list, a.base, a.len + b.len)
|
||||
del self.pending[i + 1]
|
||||
|
||||
# Where does b start in a? Elements in a before that can be
|
||||
# ignored (already in place).
|
||||
k = self.gallop(b.getitem(b.base), a, hint=0, rightmost=True)
|
||||
a.advance(k)
|
||||
if a.len == 0:
|
||||
return
|
||||
|
||||
# Where does a end in b? Elements in b after that can be
|
||||
# ignored (already in place).
|
||||
b.len = self.gallop(a.getitem(a.base + a.len - 1), b, hint=b.len - 1,
|
||||
rightmost=False)
|
||||
if b.len == 0:
|
||||
return
|
||||
|
||||
# Merge what remains of the runs. The direction is chosen to
|
||||
# minimize the temporary storage needed.
|
||||
if a.len <= b.len:
|
||||
self.merge_lo(a, b)
|
||||
else:
|
||||
self.merge_hi(a, b)
|
||||
|
||||
# Examine the stack of runs waiting to be merged, merging adjacent runs
|
||||
# until the stack invariants are re-established:
|
||||
#
|
||||
# 1. len[-3] > len[-2] + len[-1]
|
||||
# 2. len[-2] > len[-1]
|
||||
#
|
||||
# Note these invariants will not hold for the entire pending array even
|
||||
# after this function completes. [1] This does not affect the
|
||||
# correctness of the overall algorithm.
|
||||
#
|
||||
# [1] http://envisage-project.eu/proving-android-java-and-python-sorting-algorithm-is-broken-and-how-to-fix-it/
|
||||
#
|
||||
# See listsort.txt for more info.
|
||||
|
||||
def merge_collapse(self):
|
||||
p = self.pending
|
||||
while len(p) > 1:
|
||||
if len(p) >= 3 and p[-3].len <= p[-2].len + p[-1].len:
|
||||
if p[-3].len < p[-1].len:
|
||||
self.merge_at(-3)
|
||||
else:
|
||||
self.merge_at(-2)
|
||||
elif p[-2].len <= p[-1].len:
|
||||
self.merge_at(-2)
|
||||
else:
|
||||
break
|
||||
|
||||
# Regardless of invariants, merge all runs on the stack until only one
|
||||
# remains. This is used at the end of the mergesort.
|
||||
|
||||
def merge_force_collapse(self):
|
||||
p = self.pending
|
||||
while len(p) > 1:
|
||||
if len(p) >= 3 and p[-3].len < p[-1].len:
|
||||
self.merge_at(-3)
|
||||
else:
|
||||
self.merge_at(-2)
|
||||
|
||||
# Compute a good value for the minimum run length; natural runs shorter
|
||||
# than this are boosted artificially via binary insertion.
|
||||
#
|
||||
# If n < 64, return n (it's too small to bother with fancy stuff).
|
||||
# Else if n is an exact power of 2, return 32.
|
||||
# Else return an int k, 32 <= k <= 64, such that n/k is close to, but
|
||||
# strictly less than, an exact power of 2.
|
||||
#
|
||||
# See listsort.txt for more info.
|
||||
|
||||
def merge_compute_minrun(self, n):
|
||||
r = 0 # becomes 1 if any 1 bits are shifted off
|
||||
while n >= 64:
|
||||
r |= n & 1
|
||||
n >>= 1
|
||||
return n + r
|
||||
|
||||
# ____________________________________________________________
|
||||
# Entry point.
|
||||
|
||||
def sort(self):
|
||||
remaining = ListSlice(self.list, 0, self.listlength)
|
||||
if remaining.len < 2:
|
||||
return
|
||||
|
||||
# March over the array once, left to right, finding natural runs,
|
||||
# and extending short natural runs to minrun elements.
|
||||
self.merge_init()
|
||||
minrun = self.merge_compute_minrun(remaining.len)
|
||||
|
||||
while remaining.len > 0:
|
||||
# Identify next run.
|
||||
run, descending = self.count_run(remaining)
|
||||
if descending:
|
||||
run.reverse()
|
||||
# If short, extend to min(minrun, nremaining).
|
||||
if run.len < minrun:
|
||||
sorted = run.len
|
||||
run.len = min(minrun, remaining.len)
|
||||
self.binarysort(run, sorted)
|
||||
# Advance remaining past this run.
|
||||
remaining.advance(run.len)
|
||||
# Push run onto pending-runs stack, and maybe merge.
|
||||
self.pending.append(run)
|
||||
self.merge_collapse()
|
||||
|
||||
assert remaining.base == self.listlength
|
||||
|
||||
self.merge_force_collapse()
|
||||
assert len(self.pending) == 1
|
||||
assert self.pending[0].base == 0
|
||||
assert self.pending[0].len == self.listlength
|
||||
|
||||
class ListSlice:
|
||||
"A sublist of a list."
|
||||
|
||||
def __init__(self, list, base, len):
|
||||
self.list = list
|
||||
self.base = base
|
||||
self.len = len
|
||||
|
||||
def copyitems(self):
|
||||
"Make a copy of the slice of the original list."
|
||||
start = self.base
|
||||
stop = self.base + self.len
|
||||
assert 0 <= start <= stop # annotator hint
|
||||
return ListSlice(getitem_slice(self.list, start, stop), 0, self.len)
|
||||
|
||||
def advance(self, n):
|
||||
self.base += n
|
||||
self.len -= n
|
||||
|
||||
def getitem(self, item):
|
||||
return getitem(self.list, item)
|
||||
|
||||
def setitem(self, item, value):
|
||||
setitem(self.list, item, value)
|
||||
|
||||
def popleft(self):
|
||||
result = getitem(self.list, self.base)
|
||||
self.base += 1
|
||||
self.len -= 1
|
||||
return result
|
||||
|
||||
def popright(self):
|
||||
self.len -= 1
|
||||
return getitem(self.list, self.base + self.len)
|
||||
|
||||
def reverse(self):
|
||||
"Reverse the slice in-place."
|
||||
list = self.list
|
||||
lo = self.base
|
||||
hi = lo + self.len - 1
|
||||
while lo < hi:
|
||||
list_hi = getitem(list, hi)
|
||||
list_lo = getitem(list, lo)
|
||||
setitem(list, lo, list_hi)
|
||||
setitem(list, hi, list_lo)
|
||||
lo += 1
|
||||
hi -= 1
|
||||
|
||||
return TimSort
|
||||
|
||||
|
||||
ts = make_timsort_class()
|
||||
|
||||
|
||||
def tim_sort_rpython(array: list):
|
||||
ts(array).sort()
|
||||
+356
@@ -0,0 +1,356 @@
|
||||
"""
|
||||
Source code from https://gist.github.com/vladris/13bf84513e76b75a60b0eb761207541e
|
||||
|
||||
Python Timsort implementation based on the OpenJDK Java implementation
|
||||
"""
|
||||
|
||||
MIN_MERGE = 32
|
||||
MIN_GALLOP = 7
|
||||
|
||||
minGallop = MIN_GALLOP
|
||||
|
||||
|
||||
def minRunLength(n):
|
||||
r = 0
|
||||
while n >= MIN_MERGE:
|
||||
r |= n & 1
|
||||
n >>= 1
|
||||
return n + r
|
||||
|
||||
|
||||
def binarySort(arr, lo, hi, start):
|
||||
if start == lo:
|
||||
start += 1
|
||||
|
||||
while start < hi:
|
||||
pivot = arr[start]
|
||||
left, right = lo, start
|
||||
|
||||
while left < right:
|
||||
mid = (left + right) // 2
|
||||
if pivot < arr[mid]:
|
||||
right = mid
|
||||
else:
|
||||
left = mid + 1
|
||||
|
||||
arr.pop(start)
|
||||
arr.insert(left, pivot)
|
||||
|
||||
start += 1
|
||||
|
||||
|
||||
def reverseRange(arr, lo, hi):
|
||||
hi -= 1
|
||||
while lo < hi:
|
||||
arr[lo], arr[hi] = arr[hi], arr[lo]
|
||||
lo += 1
|
||||
hi -= 1
|
||||
|
||||
|
||||
def countRunAndMakeAscending(arr, lo, hi):
|
||||
runHi = lo + 1
|
||||
if runHi == hi:
|
||||
return 1
|
||||
|
||||
if arr[lo] > arr[runHi]: # Descending run
|
||||
while runHi < hi and arr[runHi] < arr[runHi - 1]:
|
||||
runHi += 1
|
||||
reverseRange(arr, lo, runHi)
|
||||
else: # Ascending run
|
||||
while runHi < hi and arr[runHi] >= arr[runHi - 1]:
|
||||
runHi += 1
|
||||
|
||||
return runHi - lo
|
||||
|
||||
|
||||
def gallopLeft(key, arr, base, len, hint):
|
||||
lastOfs, ofs = 0, 1
|
||||
if key > arr[base + hint]:
|
||||
maxOfs = len - hint
|
||||
while ofs < maxOfs and key > arr[base + hint + ofs]:
|
||||
lastOfs = ofs
|
||||
ofs = (ofs << 1) + 1
|
||||
|
||||
if ofs > maxOfs:
|
||||
ofs = maxOfs
|
||||
|
||||
lastOfs += hint
|
||||
ofs += hint
|
||||
else:
|
||||
maxOfs = hint + 1
|
||||
while ofs < maxOfs and key <= arr[base + hint - ofs]:
|
||||
lastOfs = ofs
|
||||
ofs = (ofs << 1) + 1
|
||||
|
||||
if ofs > maxOfs:
|
||||
ofs = maxOfs
|
||||
|
||||
lastOfs, ofs = hint - ofs, hint - lastOfs
|
||||
|
||||
lastOfs += 1
|
||||
while lastOfs < ofs:
|
||||
mid = lastOfs + (ofs - lastOfs) // 2
|
||||
|
||||
if key > arr[base + mid]:
|
||||
lastOfs = mid + 1
|
||||
else:
|
||||
ofs = mid
|
||||
|
||||
return ofs
|
||||
|
||||
|
||||
def gallopRight(key, arr, base, len, hint):
|
||||
ofs, lastOfs = 1, 0
|
||||
|
||||
if key < arr[base + hint]:
|
||||
maxOfs = hint + 1
|
||||
while ofs < maxOfs and key < arr[base + hint - ofs]:
|
||||
lastOfs = ofs
|
||||
ofs = (ofs << 1) + 1
|
||||
|
||||
if ofs > maxOfs:
|
||||
ofs = maxOfs
|
||||
|
||||
lastOfs, ofs = hint - ofs, hint - lastOfs
|
||||
else:
|
||||
maxOfs = len - hint
|
||||
while ofs < maxOfs and key >= arr[base + hint + ofs]:
|
||||
lastOfs = ofs
|
||||
ofs = (ofs << 1) + 1
|
||||
|
||||
if ofs > maxOfs:
|
||||
ofs = maxOfs
|
||||
|
||||
lastOfs += hint;
|
||||
ofs += hint;
|
||||
|
||||
lastOfs += 1
|
||||
while lastOfs < ofs:
|
||||
mid = lastOfs + ((ofs - lastOfs) // 2)
|
||||
if key < arr[base + mid]:
|
||||
ofs = mid
|
||||
else:
|
||||
lastOfs = mid + 1
|
||||
return ofs
|
||||
|
||||
|
||||
def mergeLo(arr, lo, mid, hi):
|
||||
t = arr[lo:mid]
|
||||
i, j, k = lo, mid, 0
|
||||
global minGallop
|
||||
done = False
|
||||
|
||||
while not done:
|
||||
count1, count2 = 0, 0
|
||||
while (count1 | count2) < minGallop:
|
||||
if t[k] < arr[j]:
|
||||
arr[i] = t[k]
|
||||
count1 += 1
|
||||
count2 = 0
|
||||
k += 1
|
||||
else:
|
||||
arr[i] = arr[j]
|
||||
count1 = 0
|
||||
count2 += 1
|
||||
j += 1
|
||||
i += 1
|
||||
|
||||
if k == mid - lo or j == hi:
|
||||
done = True
|
||||
break
|
||||
|
||||
if done:
|
||||
break
|
||||
|
||||
while count1 >= MIN_GALLOP or count2 >= MIN_GALLOP:
|
||||
count1 = gallopRight(arr[j], t, k, mid - lo - k, 0)
|
||||
if count1 != 0:
|
||||
arr[i:i + count1] = t[k:k + count1]
|
||||
i += count1
|
||||
k += count1
|
||||
if k == mid - lo:
|
||||
done = True
|
||||
break
|
||||
|
||||
arr[i] = arr[j]
|
||||
i += 1
|
||||
j += 1
|
||||
if j == hi:
|
||||
done = True
|
||||
break
|
||||
|
||||
count2 = gallopLeft(t[k], arr, j, hi - j, 0)
|
||||
if count2 != 0:
|
||||
arr[i:i + count2] = arr[j:j + count2]
|
||||
i += count2
|
||||
j += count2
|
||||
if j == hi:
|
||||
done = True
|
||||
break
|
||||
|
||||
arr[i] = t[k]
|
||||
i += 1
|
||||
k += 1
|
||||
if k == mid - lo:
|
||||
done = True
|
||||
break
|
||||
|
||||
minGallop -= 1
|
||||
|
||||
if minGallop < 0:
|
||||
minGallop = 0
|
||||
minGallop += 2
|
||||
|
||||
if k < mid - lo:
|
||||
arr[i:hi] = t[k:mid - lo]
|
||||
|
||||
|
||||
def mergeHi(arr, lo, mid, hi):
|
||||
t = arr[mid:hi]
|
||||
i, j, k = hi - 1, mid - 1, hi - mid - 1
|
||||
global minGallop
|
||||
done = False
|
||||
|
||||
while not done:
|
||||
count1, count2 = 0, 0
|
||||
while (count1 | count2) < minGallop:
|
||||
if t[k] > arr[j]:
|
||||
arr[i] = t[k]
|
||||
count1 += 1
|
||||
count2 = 0
|
||||
k -= 1
|
||||
else:
|
||||
arr[i] = arr[j]
|
||||
count1 = 0
|
||||
count2 += 1
|
||||
j -= 1
|
||||
i -= 1
|
||||
|
||||
if k == -1 or j == lo - 1:
|
||||
done = True
|
||||
break
|
||||
|
||||
if done:
|
||||
break
|
||||
|
||||
while count1 >= MIN_GALLOP or count2 >= MIN_GALLOP:
|
||||
count1 = j - lo + 1 - gallopRight(t[k], arr, lo, j - lo + 1, j - lo)
|
||||
if count1 != 0:
|
||||
arr[i - count1 + 1:i + 1] = arr[j - count1 + 1:j + 1]
|
||||
i -= count1
|
||||
j -= count1
|
||||
if j == lo - 1:
|
||||
done = True
|
||||
break
|
||||
|
||||
arr[i] = t[k]
|
||||
i -= 1
|
||||
k -= 1
|
||||
if k == -1:
|
||||
done = True
|
||||
break
|
||||
|
||||
count2 = k + 1 - gallopLeft(arr[j], t, 0, k + 1, k)
|
||||
if count2 != 0:
|
||||
arr[i - count2 + 1:i + 1] = t[k - count2 + 1:k + 1]
|
||||
i -= count2
|
||||
k -= count2
|
||||
if k == -1:
|
||||
done = True
|
||||
break
|
||||
|
||||
arr[i] = arr[j]
|
||||
i -= 1
|
||||
j -= 1
|
||||
if j == lo - 1:
|
||||
done = True
|
||||
break
|
||||
|
||||
minGallop -= 1
|
||||
|
||||
if minGallop < 0:
|
||||
minGallop = 0
|
||||
minGallop += 2
|
||||
|
||||
if k >= 0:
|
||||
arr[lo:i + 1] = t[0:k + 1]
|
||||
|
||||
|
||||
def mergeAt(arr, stack, i):
|
||||
assert i == len(stack) - 2 or i == len(stack) - 3
|
||||
|
||||
base1, len1 = stack[i]
|
||||
base2, len2 = stack[i + 1]
|
||||
|
||||
stack[i] = (base1, len1 + len2)
|
||||
if i == len(stack) - 3:
|
||||
stack[i + 1] = stack[i + 2]
|
||||
stack.pop()
|
||||
|
||||
k = gallopRight(arr[base2], arr, base1, len1, 0)
|
||||
base1 += k
|
||||
len1 -= k
|
||||
if len1 == 0:
|
||||
return
|
||||
|
||||
len2 = gallopLeft(arr[base1 + len1 - 1], arr, base2, len2, len2 - 1)
|
||||
if len2 == 0:
|
||||
return
|
||||
|
||||
if len1 > len2:
|
||||
mergeLo(arr, base1, base2, base2 + len2)
|
||||
else:
|
||||
mergeHi(arr, base1, base2, base2 + len2)
|
||||
|
||||
|
||||
def mergeCollapse(arr, stack):
|
||||
while len(stack) > 1:
|
||||
n = len(stack) - 2
|
||||
if (n > 0 and stack[n - 1][1] <= stack[n][1] + stack[n + 1][1]) or \
|
||||
(n > 1 and stack[n - 2][1] <= stack[n - 1][1] + stack[n][1]):
|
||||
if stack[n - 1][1] < stack[n + 1][1]:
|
||||
n -= 1
|
||||
elif n < 0 or stack[n][1] > stack[n + 1][1]:
|
||||
break
|
||||
|
||||
mergeAt(arr, stack, n)
|
||||
|
||||
|
||||
def mergeForceCollapse(arr, stack):
|
||||
while len(stack) > 1:
|
||||
n = len(stack) - 2
|
||||
if n > 0 and stack[n - 1][1] < stack[n + 1][1]:
|
||||
n -= 1
|
||||
|
||||
mergeAt(arr, stack, n)
|
||||
|
||||
|
||||
def tim_sort_vladris(arr):
|
||||
lo, hi = 0, len(arr)
|
||||
stack = []
|
||||
nRemaining = hi
|
||||
global minGallop
|
||||
minGallop = MIN_GALLOP
|
||||
|
||||
if nRemaining < MIN_MERGE:
|
||||
initRunLen = countRunAndMakeAscending(arr, lo, hi)
|
||||
binarySort(arr, lo, hi, lo + initRunLen)
|
||||
return
|
||||
|
||||
minRun = minRunLength(len(arr))
|
||||
|
||||
while nRemaining > 0:
|
||||
runLen = countRunAndMakeAscending(arr, lo, hi)
|
||||
|
||||
if runLen < minRun:
|
||||
force = min(nRemaining, minRun)
|
||||
binarySort(arr, lo, lo + force, lo + runLen)
|
||||
runLen = force
|
||||
|
||||
stack.append((lo, runLen))
|
||||
mergeCollapse(arr, stack)
|
||||
|
||||
lo += runLen
|
||||
nRemaining -= runLen
|
||||
|
||||
mergeForceCollapse(arr, stack)
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
"""
|
||||
QuickSort example
|
||||
Source code from https://www.geeksforgeeks.org/iterative-quick-sort/ (code is contributed by Mohit Kumra)
|
||||
"""
|
||||
|
||||
|
||||
def partition(array: list, low: int, high: int):
|
||||
i = (low - 1)
|
||||
x = array[high]
|
||||
|
||||
for j in range(low, high):
|
||||
if array[j] <= x:
|
||||
# increment index of smaller element
|
||||
i = i + 1
|
||||
array[i], array[j] = array[j], array[i]
|
||||
|
||||
array[i + 1], array[high] = array[high], array[i + 1]
|
||||
return i + 1
|
||||
|
||||
|
||||
# Function to do Quick sort
|
||||
# arr[] --> Array to be sorted,
|
||||
# l --> Starting index,
|
||||
# h --> Ending index
|
||||
def quick_sort_but_slow(array: list):
|
||||
# Create an auxiliary stack
|
||||
low = 0
|
||||
high = len(array) - 1
|
||||
size = high - low + 1
|
||||
stack = [0] * size
|
||||
|
||||
# initialize top of stack
|
||||
top = -1
|
||||
|
||||
# push initial values of l and h to stack
|
||||
top = top + 1
|
||||
stack[top] = low
|
||||
top = top + 1
|
||||
stack[top] = high
|
||||
|
||||
# Keep popping from stack while is not empty
|
||||
while top >= 0:
|
||||
# Pop h and l
|
||||
high = stack[top]
|
||||
top = top - 1
|
||||
low = stack[top]
|
||||
top = top - 1
|
||||
|
||||
# Set pivot element at its correct position in
|
||||
# sorted array
|
||||
p = partition(array, low, high)
|
||||
|
||||
# If there are elements on left side of pivot,
|
||||
# then push left side to stack
|
||||
if p - 1 > low:
|
||||
top = top + 1
|
||||
stack[top] = low
|
||||
top = top + 1
|
||||
stack[top] = p - 1
|
||||
|
||||
# If there are elements on right side of pivot,
|
||||
# then push right side to stack
|
||||
if p + 1 < high:
|
||||
top = top + 1
|
||||
stack[top] = p + 1
|
||||
top = top + 1
|
||||
stack[top] = high
|
||||
|
||||
|
||||
"""
|
||||
Iterative implementation of quick_sort
|
||||
Source code from https://stackoverflow.com/questions/66546476/non-recursive-quicksort
|
||||
User https://stackoverflow.com/users/3282056/rcgldr
|
||||
"""
|
||||
|
||||
|
||||
def quick_sort(array):
|
||||
if len(array) < 2: # if nothing to sort, return
|
||||
return
|
||||
stack = [[0, len(array) - 1]] # initialize stack
|
||||
while len(stack) > 0: # loop till stack empty
|
||||
lo, hi = stack.pop() # pop lo, hi indexes
|
||||
p = array[(lo + hi) // 2] # pivot, any a[] except a[hi]
|
||||
i = lo - 1 # Hoare partition
|
||||
j = hi + 1
|
||||
while 1:
|
||||
while 1: # while(a[++i] < p)
|
||||
i += 1
|
||||
if array[i] >= p:
|
||||
break
|
||||
while 1: # while(a[--j] < p)
|
||||
j -= 1
|
||||
if array[j] <= p:
|
||||
break
|
||||
if i >= j: # if indexes met or crossed, break
|
||||
break
|
||||
array[i], array[j] = array[j], array[i] # else swap elements
|
||||
if j > lo: # push indexes onto stack
|
||||
stack.append([lo, j])
|
||||
j += 1
|
||||
if hi > j:
|
||||
stack.append([j, hi])
|
||||
+119
@@ -0,0 +1,119 @@
|
||||
"""
|
||||
Created on Dec 20, 2016
|
||||
|
||||
@author: Arthur
|
||||
"""
|
||||
from random import *
|
||||
from datetime import *
|
||||
from texttable import *
|
||||
|
||||
from ex11_insertion_sort import insertion_sort, binary_insertion_sort
|
||||
from ex12_merge_sort import merge_sort
|
||||
from ex13_tim_sort import tim_sort_rpython
|
||||
from ex14_tim_sort import tim_sort_vladris
|
||||
from ex15_quick_sort import quick_sort_but_slow, quick_sort
|
||||
|
||||
|
||||
def already_sorted_data(data_size):
|
||||
result = list(range(0, data_size))
|
||||
return result
|
||||
|
||||
|
||||
def sorted_in_reverse_data(data_size):
|
||||
result = list(range(data_size, 0, -1))
|
||||
return result
|
||||
|
||||
|
||||
def random_data(data_size):
|
||||
result = list(range(0, data_size))
|
||||
shuffle(result)
|
||||
return result
|
||||
|
||||
|
||||
def test_sorts():
|
||||
array = list(range(100, 0, -1))
|
||||
insertion_sort(array)
|
||||
assert array == list(range(1, 101))
|
||||
|
||||
array.reverse()
|
||||
array = binary_insertion_sort(array)
|
||||
assert array == list(range(1, 101))
|
||||
|
||||
array.reverse()
|
||||
merge_sort(array)
|
||||
assert array == list(range(1, 101))
|
||||
|
||||
array.reverse()
|
||||
quick_sort_but_slow(array)
|
||||
assert array == list(range(1, 101))
|
||||
|
||||
array.reverse()
|
||||
tim_sort_rpython(array)
|
||||
assert array == list(range(1, 101))
|
||||
|
||||
array.reverse()
|
||||
tim_sort_vladris(array)
|
||||
assert array == list(range(1, 101))
|
||||
|
||||
|
||||
test_sorts()
|
||||
|
||||
'''
|
||||
Utility function to convert a timedelta into a number of milliseconds
|
||||
'''
|
||||
|
||||
|
||||
def millis_interval(start, end):
|
||||
diff = end - start
|
||||
millis = diff.days * 24 * 60 * 60 * 1000
|
||||
millis += diff.seconds * 1000
|
||||
millis += diff.microseconds / 1000
|
||||
return int(millis)
|
||||
|
||||
|
||||
'''
|
||||
And here we build our experiment
|
||||
|
||||
data_generators - Change the functions that build the data set to be sorted
|
||||
sort_functions - What functions will do the actual sort
|
||||
data_sizes - What are the data sizes to be sorted
|
||||
'''
|
||||
|
||||
|
||||
def sort_test():
|
||||
"""
|
||||
Generator functions for best case, average case and worst case
|
||||
"""
|
||||
data_generators = [already_sorted_data, random_data, sorted_in_reverse_data]
|
||||
|
||||
'''
|
||||
Sorting functions to employ
|
||||
'''
|
||||
sort_functions = [insertion_sort, binary_insertion_sort, merge_sort, quick_sort_but_slow, quick_sort,
|
||||
tim_sort_rpython, tim_sort_vladris, sorted]
|
||||
|
||||
'''
|
||||
Data sizes that will be sorted
|
||||
'''
|
||||
data_sizes = [64, 128, 256, 512, 1024, 2048, 4096, 8192]
|
||||
|
||||
'''
|
||||
Do the sort and build the result table dynamically
|
||||
'''
|
||||
for generator in data_generators:
|
||||
print("Current data: " + generator.__name__)
|
||||
t = Texttable()
|
||||
t.add_row(['Functions/size'] + data_sizes)
|
||||
for sort_function in sort_functions:
|
||||
row = [sort_function.__name__]
|
||||
for size in data_sizes:
|
||||
data = generator(size)
|
||||
t1 = datetime.now()
|
||||
sort_function(data)
|
||||
t2 = datetime.now()
|
||||
row = row + [millis_interval(t1, t2)]
|
||||
t.add_row(row)
|
||||
print(t.draw())
|
||||
|
||||
|
||||
sort_test()
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
"""
|
||||
Created on Jan 10, 2017
|
||||
|
||||
@author: Arthur
|
||||
"""
|
||||
|
||||
import time
|
||||
from texttable import *
|
||||
|
||||
|
||||
def generate_test(array, dim):
|
||||
if len(array) == dim:
|
||||
# print (array)
|
||||
pass
|
||||
if len(array) > dim:
|
||||
return
|
||||
array.append(0)
|
||||
for i in range(0, dim):
|
||||
array[-1] = i
|
||||
generate_test(array[:], dim)
|
||||
|
||||
|
||||
def backtracking_iter(dim: int):
|
||||
array = [-1] # candidate solution
|
||||
while len(array) > 0:
|
||||
chosen = False
|
||||
while not chosen and array[-1] < dim - 1:
|
||||
array[-1] = array[-1] + 1 # increase the last component
|
||||
chosen = len(set(array)) == len(array)
|
||||
if chosen:
|
||||
if len(array) == dim:
|
||||
print(array)
|
||||
array.append(-1) # expand candidate solution
|
||||
else:
|
||||
array = array[:-1] # go back one component
|
||||
|
||||
|
||||
def backtracking_rec(array, dim):
|
||||
if len(array) == dim:
|
||||
print(array)
|
||||
if len(array) > dim:
|
||||
return
|
||||
array.append(0)
|
||||
for i in range(0, dim):
|
||||
array[-1] = i
|
||||
if len(set(array)) == len(array):
|
||||
backtracking_rec(array, dim)
|
||||
array.pop()
|
||||
|
||||
|
||||
'''
|
||||
And here we build our experiment
|
||||
'''
|
||||
functions = [generate_test, backtracking_rec]
|
||||
data_sizes = [3, 4, 5, 6, 7]
|
||||
|
||||
t = Texttable()
|
||||
t.add_row(['Functions'] + data_sizes)
|
||||
for function in functions:
|
||||
row = [function.__name__]
|
||||
for size in data_sizes:
|
||||
t1 = time.perf_counter()
|
||||
function([], size)
|
||||
t2 = time.perf_counter()
|
||||
row = row + [t2 - t1]
|
||||
t.add_row(row)
|
||||
print(t.draw())
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user