diff --git a/adder.py b/adder.py new file mode 100644 index 0000000..97054d7 --- /dev/null +++ b/adder.py @@ -0,0 +1,9 @@ +import sys + +if __name__ == "__main__": + result = 0 + + for arg in sys.argv: + if(arg.isdecimal()): + result += int(arg) + print(result) \ No newline at end of file diff --git a/args.py b/args.py new file mode 100644 index 0000000..cde3aec --- /dev/null +++ b/args.py @@ -0,0 +1,6 @@ +import sys + +if __name__ == "__main__": + print(f"Arguments count: {len(sys.argv)}") + for i, arg in enumerate(sys.argv): + print(f"Argument {i:>6}: {arg}") \ No newline at end of file diff --git a/class-incest.py b/class-incest.py new file mode 100644 index 0000000..747486a --- /dev/null +++ b/class-incest.py @@ -0,0 +1,35 @@ +class Root(): + a = "Root" + def f(self): + print(self.a) + pass + +class A(Root): + a = "A" + def f(self): + print(self.a) + super().f() + pass + +class B(Root): + a = "B" + def f(self): + print(self.a) + super().f() + pass + +class C(A, B): + # C is a child of A and B, which are siblings. Therefore incest. + # You may laugh now. + def f(self): + print("C") + super().f() + print(C.__mro__) # Method Resolution Order(MRO) + pass + +def main(): + o = C() + o.f() + +if __name__ == "__main__": + main() diff --git a/hello.py b/hello.py new file mode 100644 index 0000000..0f31502 --- /dev/null +++ b/hello.py @@ -0,0 +1,6 @@ +print("hello world!\n") +for i in range(10): + if(i<9): + print((i)*"hello-"+"hello") + elif(i==0): + print("hello") \ No newline at end of file diff --git a/randomString.py b/randomString.py new file mode 100644 index 0000000..f4285d9 --- /dev/null +++ b/randomString.py @@ -0,0 +1,11 @@ +import random +import string + + +def get_random_string(length): + # choose from all lowercase letter + letters = string.ascii_lowercase + result_str = ''.join(random.choice(letters) for i in range(length)) + return result_str + +print(get_random_string(20)) diff --git a/simple-calc.py b/simple-calc.py new file mode 100644 index 0000000..decb6a3 --- /dev/null +++ b/simple-calc.py @@ -0,0 +1,65 @@ +import sys + +if __name__ == "__main__": + result = 0 + + def help(): { + print("input: [value] [operation] [value]\noperations: [ + - . / ]\n") + } + + if(len(sys.argv) > 1): + if(sys.argv[1] == "-h" or sys.argv[1] == "--help"): + help() + sys.exit() + if(len(sys.argv) == 0): + help() + sys.exit() + + if(len(sys.argv) < 4): + print("not enough arguments\n") + help() + sys.exit() + if(len(sys.argv) > 4): + print("too many arguments\n") + help() + sys.exit() + + if(sys.argv[1].isdecimal): + val1=int(sys.argv[1]) + + else: + print("1st arg should be an integer!") + help() + sys.exit() + if(sys.argv[2].isascii and len(sys.argv[2]) == 1): + if (not ((sys.argv[2]=="+") or (sys.argv[2]=="-") or (sys.argv[2]==".") or (sys.argv[2]=="/"))): + print("2nd arg should be an operator [ + - . / ]!\n") + help() + sys.exit() + op=sys.argv[2] + + if(sys.argv[3].isdecimal): + val2=int(sys.argv[3]) + else: + print("3rd arg should be an integer!") + help() + + + #todo switch case and then operations + + if(op=="+"): + print(f"{val1} + {val2} = {val1+val2}") + + elif(op=="-"): + print(f"{val1} - {val2} = {val1-val2}") + + elif(op=="."): + print(f"{val1} . {val2} = {val1*val2}") + + elif(op=="/"): + print(f"{val1} / {val2} = {val1/val2}") + + else: + print(f"'{op}' is not a valid operator") + sys.exit() + \ No newline at end of file