Skip to content

Instantly share code, notes, and snippets.

@ChanChar
Created February 7, 2015 19:18
Show Gist options
  • Save ChanChar/c41a50b18870b47a6015 to your computer and use it in GitHub Desktop.
Save ChanChar/c41a50b18870b47a6015 to your computer and use it in GitHub Desktop.
2/7/2015 Solve Saturday Q/A
# Modular Calculator
# I looked through a couple of other people's answers and it seems that most people had used a while loop.
# There is a much simpler solution using dictionaries/hash tables.
# http://www.codeabbey.com/index/task_view/modular-calculator
total = int(input()) # initial value
solved = False
while not solved:
operation, value = input().split()
if operation == "+":
total += int(value)
elif operation == "*":
total *= int(value)
else:
solved = True
print(total % int(value))
# Collatz Sequence
# This solution uses a bit of recursive programming. The function continues to call itself
# until a solution has been found. There are also solutions that do not involve any sort of recursion.
# http://www.codeabbey.com/index/task_view/collatz-sequence
def collatz(number, count=0):
current_count = count
if number == 1:
print(count, end=' ')
elif number % 2 == 0:
new_number = number/2
current_count += 1
collatz(new_number, current_count)
else:
new_number = 3 * number + 1
current_count += 1
collatz(new_number, current_count)
number_of_cases = int(input())
for new_number in [int(i) for i in input().split()]:
collatz(new_number)
# Arithmetic Progression
# http://www.codeabbey.com/index/task_view/arithmetic-progression
# Remember you can set multiple variables at a time if you have the exact number of arguments.
def arithmetic_progression(a,b,n):
total = 0
for i in range(0,n):
total += (a + b*i)
return total
number_of_cases = int(input())
for case in range(number_of_cases):
a,b,n = [int(i) for i in input().split()] # Like this.
print(arithmetic_progression(a,b,n),end=' ')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment