Skip to content

Instantly share code, notes, and snippets.

View odanga94's full-sized avatar

Odanga Ochieng' odanga94

  • Kenya
View GitHub Profile
@odanga94
odanga94 / Exercise5.py
Created May 12, 2017 08:55
PracticePython/Exercise5
from random import randint
import sys
# Part 1
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
c = []
def merge():
for item in a:
if item in b and item not in c:
c.append(item)
@odanga94
odanga94 / Exercise6.py
Created May 12, 2017 10:57
PracticePython/Exercise6
string = raw_input("Enter the string to test:")
string = string.lower()
backwards = []
index = len(string) - 1
while index >= 0:
backwards.append(string[index])
index -= 1
string_backwards = "".join(backwards)
if string == string_backwards:
print(True)
@odanga94
odanga94 / Exercise7.py
Created May 12, 2017 11:17
PracticePython/Exercise7
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
c = [x for x in a if x % 2 == 0]
print(c)
@odanga94
odanga94 / Exercise8.py
Created May 12, 2017 13:52
PracticePython/Exercise8(rock, paper, scissors cmd line)
def validate_input(player_choice):
if player_choice != "r" and player_choice != "p" and\
player_choice != "s":
print("The input was invalid. Please input 'r', 'p' or 's'")
return False
else:
return True
def winner(player_1_choice, player_2_choice):
if player_1_choice == player_2_choice:
@odanga94
odanga94 / Exercise9.py
Created May 12, 2017 15:24
PracticePython/Exercise9
from random import randint
true_value = randint(1, 9)
print(true_value)
count = 0
while True:
user_guess = int(input("Guess a number between 1 and 9: "))
count += 1
if user_guess == true_value:
print("You guess was correct. Congratulations!!")
break
@odanga94
odanga94 / Exercise11.py
Created May 12, 2017 16:44
PracticePython/Exercise11.py
def is_prime():
num = int(input("Enter the number you wanna check:"))
divisor = 2
while num != divisor:
if num == 1:
print(False)
break
elif num % divisor == 0:
print(False)
break
@odanga94
odanga94 / Exercise12.py
Created May 13, 2017 07:11
PracticePython/Exercise12
a = [0, 5, 10, 15, 20, 25, 75]
def new_list(a):
new = []
new.append(a[0])
new.append(a[len(a) - 1])
print(new)
new_list(a)
@odanga94
odanga94 / Exercise13.py
Created May 13, 2017 08:14
Fiboncacci/Exercise13.py
def fibonacci(): # where n is the nth number of the Fibonacci sequence the function will generate
fib_list = []
fib_1 = 1 # by definition the first two numbers in the Fibonacci sequence are both 1
n = int(input("How many numbers in the Fibonacci sequence would you like to generate? "))
if n == 1 or n ==2:
for i in range(n):
fib_list.append(fib_1)
else:
for i in range(2):
@odanga94
odanga94 / Exercise15.py
Created May 13, 2017 15:59
PracticePython/Exercise15.py
def reverse():
original = raw_input("Enter a string containing multiple words:")
reverse = " ".join(original.split()[::-1])
print(reverse)
reverse()
@odanga94
odanga94 / Exercise16.py
Created May 14, 2017 12:07
PracticePython/Exercise16
import random
import string
lower_case = set(string.ascii_lowercase)
upper_case = set(string.ascii_uppercase)
digits = set(string.digits)
special_char = set(string.punctuation)
def password_generator():
length = int(raw_input("How long a password do you want (at least 4)? "))
password = set(random.sample((lower_case | upper_case | digits | special_char), length))