forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added b16, b32, a85, abs, absMax, absMin
- Loading branch information
1 parent
5492681
commit a834326
Showing
9 changed files
with
103 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
def absVal(num): | ||
""" | ||
Function to fins absolute value of numbers. | ||
>>>absVal(-5) | ||
5 | ||
>>>absVal(0) | ||
0 | ||
""" | ||
if num < 0: | ||
return -num | ||
else: | ||
return num | ||
|
||
def main(): | ||
print(absVal(-34)) # = 34 | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from abs import absVal | ||
def absMax(x): | ||
""" | ||
>>>absMax([0,5,1,11]) | ||
11 | ||
>>absMax([3,-10,-2]) | ||
-10 | ||
""" | ||
j = x[0] | ||
for i in x: | ||
if absVal(i) < j: | ||
j = i | ||
return j | ||
#BUG: i is apparently a list, TypeError: '<' not supported between instances of 'list' and 'int' in absVal | ||
|
||
|
||
def main(): | ||
a = [1,2,-11] | ||
print(absVal(a)) # = -11 | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from abs import absVal | ||
def absMin(x): | ||
""" | ||
>>>absMin([0,5,1,11]) | ||
0 | ||
>>absMin([3,-10,-2]) | ||
-2 | ||
""" | ||
j = x[0] | ||
for i in x: | ||
if absVal(i) < j: | ||
j = i | ||
return j | ||
|
||
def main(): | ||
a = [1,2,-11] | ||
print(absMin(a)) # = 1 | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import base64 | ||
|
||
def main(): | ||
inp = input('->') | ||
encoded = inp.encode('utf-8') #encoded the input (we need a bytes like object) | ||
b16encoded = base64.b16encode(encoded) #b16encoded the encoded string | ||
print(b16encoded) | ||
print(base64.b16decode(b16encoded).decode('utf-8'))#decoded it | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import base64 | ||
|
||
def main(): | ||
inp = input('->') | ||
encoded = inp.encode('utf-8') #encoded the input (we need a bytes like object) | ||
b32encoded = base64.b32encode(encoded) #b32encoded the encoded string | ||
print(b32encoded) | ||
print(base64.b32decode(b32encoded).decode('utf-8'))#decoded it | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import base64 | ||
|
||
def main(): | ||
inp = input('->') | ||
encoded = inp.encode('utf-8') #encoded the input (we need a bytes like object) | ||
a85encoded = base64.a85encode(encoded) #a85encoded the encoded string | ||
print(a85encoded) | ||
print(base64.a85decode(a85encoded).decode('utf-8'))#decoded it | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
arr = [10, 20, 30, 40] | ||
arr[1] = 30 | ||
arr[1] = 30 # set element 1 (20) of array to 30 | ||
print(arr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters