Skip to content

Instantly share code, notes, and snippets.

@svmihar
Last active May 21, 2019 23:33
Show Gist options
  • Save svmihar/b186991172762877f3a6bd0e80810657 to your computer and use it in GitHub Desktop.
Save svmihar/b186991172762877f3a6bd0e80810657 to your computer and use it in GitHub Desktop.
from multiprocessing import Process, current_process
import multiprocessing
def g(str):
i = 0
new_str = ""
while i < len(str) -1:
new_str = new_str + str[i+1]
i = i+1
return new_str
def f(str):
if len(str) == 0:
return ""
elif len(str) == 1:
return str
else:
return (f(g(str)) + str[0])
def h(n, str):
i= 0
while n != 1:
i+=1
# print(f'{n}')
if n%2 == 0:
n = n/2
else:
n = 3*n + 1
str = f(str)
print(f'diulang {i} kali')
return str
def pow(x,y):
if y == 0:
return 1
else:
return x**y
def fast_power(base, power):
"""
Returns the result of a^b i.e. a**b
We assume that a >= 1 and b >= 0
Remember two things!
- Divide power by 2 and multiply base to itself (if the power is even)
- Decrement power by 1 to make it even and then follow the first step
"""
result = 1
while power > 0:
print(result)
# If power is even
if power % 2 == 0:
# Divide the power by 2
power = power // 2
# Multiply base to itself
base = base * base
else:
# Decrement the power by 1 and make it even
power = power - 1
# Take care of the extra value that we took out
# We will store it directly in result
result = result * base
# Now power is even, so we can follow our previous procedure
power = power // 2
base = base * base
return result
def main(str):
print(f"\n\n\n\n\n{h(fast_power(2, 1000000000000000), str)}")
if __name__ == "__main__":
print(g('fruits'))
print(f('asyu'))
hasil = []
# p1 = Process(target=main)
# hasil.append(p1)
# p1.start()
p = multiprocessing.Pool(processes=multiprocessing.cpu_count())
result = p.map(main,'fruit')
print(result)
#with pool
start = time.time()
p = multiprocessing.Pool(processes=multiprocessing.cpu_count())
result = p.apply_async(fast_power,args=(2, 1000000000))
# print(result)
print(type(result.get()))
print(f'run for {round(time.time() - start, 2)}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment