Skip to content

Instantly share code, notes, and snippets.

@cadrev
Last active August 29, 2015 14:26
Show Gist options
  • Save cadrev/cd4b6457654ba8ff22e4 to your computer and use it in GitHub Desktop.
Save cadrev/cd4b6457654ba8ff22e4 to your computer and use it in GitHub Desktop.

Exercise 1: Leap Year

# Get input from user
year = raw_input('Enter year: ')
year = int(year)
# Calculate leap year
if ((year % 4 == 0) and (year%100 != 0)) or (year % 400 == 0):
  print "Yes"
else:
  print 'No'

Exercise 2 : Grading System

grade = raw_input('Enter grade: ')
grade = float(grade)

if grade > 90:
  print 'A'

elif (grade >= 80) and (grade <= 90):
  print 'B'

elif (grade >= 70) and (grade <= 79):
  print 'C'

elif (grade >= 60) and (grade <= 69):
  print 'D'

else:
  print 'E'


Exercise 3 : Calculator

# Get user input
print "Choose from the ff operation: "
print "add,subtract,multiply,divide"
operation = raw_input('enter: ')


# Perform operations

if operation == 'add':
  x = raw_input('Enter first number : ')
  y = raw_input('Enter second number: ')
  print float(x) + float(y)

elif operation == 'subtract':
  x = raw_input('Enter first number : ')
  y = raw_input('Enter second number: ')
  print float(x) - float(y)

elif operation == 'divide':
  x = raw_input('Enter first number : ')
  y = raw_input('Enter second number: ')
  print float(x) / float(y)

elif operation == 'multiply':
  x = raw_input('Enter first number  : ')
  y = raw_input('Enter second number : ')
  print float(x) * float(y)

else:
  print 'operation not found :('

Exercise 4 : Factorial

# Get user input
x = int(raw_input('Enter number: '))

accumulator = 1
for i in range(1,x+1):
  accumulator = accumulator * i

print "Factorial of " + str(x) + " is = " +  str(accumulator)

Exercise 5: Fibonacci

# Get user input
input = int(raw_input('Enter number: '))

# set variables
x = 1
y = 1
temp = x

# since 0 is not included in the sequence, print 0
if input == 0:
  print "zero is not included in the sequence"
  exit()

# calculate the fibonacci number
for i in range(input-1):
   x = y
   y = temp + y
   temp = x
   
print "Fibonacci of " + str(input) + " is " + str(x)

Exercise 6 : Find the even numbers on a list

# Hardcoded list
number_list = [2,5,7,8,9,10,11,12,13,15,16,17,18]
final_list  = []

# variable holder
temp = 0

# process the list
for number in number_list:

  if number % 2 == 0:
    temp = number + 1

  else:
    temp = number

  final_list.append(temp)
  temp = 0

print final_list

Exercise 7 : Philippine Peso to USD conversion rates na hindi naman pala >__>

# hardcoded data
data = [41,42,43,44,45,45,47,48,49,50]

# important(?) variables hahaha
ui_string = "Choices: average,add,max,min,count,distinct,exit \n"
input = ""

# process user input
while input != 'exit':
  input = raw_input(ui_string)

  if input == 'average':
    print sum(data)/len(data)

  elif input == 'add':
    number = float(raw_input('Enter number to add: '))
    data.append(number)
    print 'number added'
  
  elif input == 'max':
    print max(data)

  elif input == 'min':
    print min(data)

  elif input == 'length':
    print len(data)
  
  elif input == 'count':
    number = float(raw_input('Enter a new value: '))
    print str(number) + " has " + str(data.count(number)) + " instance(s)"

  elif input == 'distinct':
    print list(set(data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment