Skip to content

Instantly share code, notes, and snippets.

@pcmac77
Created April 9, 2019 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcmac77/f0d536406607c4148d978f264e76e989 to your computer and use it in GitHub Desktop.
Save pcmac77/f0d536406607c4148d978f264e76e989 to your computer and use it in GitHub Desktop.
2019-04-11 created by a10candidate - https://repl.it/@a10candidate/2019-04-11
###########
# Fibonacci
# Write a function fib(n) that returns a Fibonacci num.
# Your solution should be performant
def fibonacci(n):
# terminal condition
if n == 0:
return 0
if n == 1 or n == 2:
return 1
if n in fib_dict:
return fib_dict[n]
# recursive
else:
fib_dict[n] = fibonacci(n-1) + fibonacci(n-2)
return fib_dict[n]
# fibonacci secuence
# 0, 1, 1, 2, 3, 5, 8, 13
#return None
import time
fib_dict = dict()
for x in [7, 10, 20, 30, 40]:
t0=time.time()
print("fibonacci({})={}".format(x, fibonacci(x)))
print("Took {} seconds".format(time.time()-t0))
print("Done with fibonacci")
# 2019-04-09, Chen En Sung
###########
# Fibonacci
print('#' * 12)
import fibonacci
###########
# Reverse Words
print('#' * 12)
import reverse_words
###########
# Simple Transposition
print('#' * 12)
import simple_transposition
# Complete the function that accepts a string parameter, # # and reverses each word in the string. All spaces in the # string should be retained.
#Examples
#"This is an example!" ==> "sihT si na !elpmaxe"
#"double spaces" ==> "elbuod secaps"
def reverse_words(input):
words_li = []
for s in input.split():
word = ''
for w in s:
word += w
word = word[::-1]
words_li.append(word)
rev_str = ' '.join(str(e) for e in words_li)
return rev_str
input="This is an example"
print(reverse_words(input))
#Simple transposition is a basic and simple cryptography #technique. We make 2 rows and put first a letter in the #Row 1, the second in the Row 2, third in Row 1 and so on #until the end. Then we put the text from Row 2 next to #the Row 1 text and thats it.
#Complete the function that recieves a string and encrypt #it with this simple transposition.
#Example
#For example if the text to encrypt is: "Simple text", the #2 rows will be:
#Row 1 S m l e t
#Row 2 i p e t x
#So the result string will be: `"Sml etipetx"`
def simple_transposition(input):
row1 = ''
row2 = ''
row1 += '"'
for w in range(len(input)):
if w % 2 == 0:
row1 += input[w]
else:
row2 += input[w]
row2 += '"'
ret_row = row1 + row2
return ret_row
input="Simple text"
print(simple_transposition(input))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment