Instantly share code, notes, and snippets.

# marioluan/censor.py Last active Aug 7, 2017

What would you like to do?
A few programs/functions I've created while learning Python from MOOC's course.
 def censor(text, word): """takes two strings 'text' and 'word' as input and returns the text with the word you chose replaced with asterisks.""" import string # we're going to use split and join built-in functions # first of all, we need to break the words into a list text = string.split(text) # now, we iterate over the list index for index in range(0,len(text)): # let's calculate when to stop # if we found the word into the list if word == text[index]: text[index] = len(word)*'*' # assign asterisks (multiplied by word's length) to the list index # when we're done with the new assignments, let's join list's elements before printing text = string.join(text) # done! return text
 """ Dice roll description: takes two parameters and generates random roll values for each die rolled. call roll_dice(x,y) to play! x = number of sides of the die y = number of dice to roll """ def random_number(x): import random r = random.randint(1, x) print r return True def roll_dice(x, y): for i in range(0, y): random_number(x) return "That's All"
 def median(x): """ takes in a sorted sequence of numbers and output the middle number """ x.sort() # after taking the list, let's sort it len_x = len(x) # now, let's get the list length # even list goes here if len_x % 2 == 0: # let's play math left_middle_index = (len_x/2)-1 right_middle_index = (len_x/2) return (x[left_middle_index]+x[right_middle_index])/2.0 # odd list goes here else: middle_index = int(round((len_x)/2)) return x[middle_index]
 def pyglatim(): """ PygLatim translator description: translate an English word into a PygLatim one eg: If the user types 'car', it will be translated to 'arcay'. If the user types 'eggs' it will be translated to 'eggsay' more info: http://en.wikipedia.org/wiki/Pig_Latin """ # PygLatim variable pyg = 'ay' # user input word = raw_input('Enter a word:\n') # checks if the input is empty if len(word) > 0: # checks if the input has only alphabetic characters if word.isalpha(): # convert to lower case letters word = word.lower() # store the first letter into a variable first = word[0] vowel = 'aeiou' # checks if the first letter is vowel for i in vowel: if i == first: new_word = word+pyg print new_word break else: new_word = word[1:]+first+pyg print new_word break else: print 'You must enter only alphabetic characters' pyglatim() else: print 'You did not enter any value!' pyglatim()
 def remove_duplicates(x): """ takes in a list and removes elements of the list that are the same. """ # let's create another list to store no duplicate elements newX = [] for i in range(0,len(x)): # stores the first element from original list while len(newX) == 0: newX.append(x[i]) # after while loop, every time the program iterates through the original list # it'll check if the element is already stored in our new list. If not, it will be stored. if x[i] not in newX: newX.append(x[i]) return newX
 def rps(x,y): """ takes two parameters: rock, paper or scissorts, and returns who won. x = player_1 y = player_2 """ # making sure the entered arguments are strings and formed only with alphabetic characters if ( (type(x) == str and type(y) == str) and (x.isalpha() == True and y.isalpha() == True) ): # checking values from arguments if x == "rock" or x == "paper" or x == "scissors": if y == "rock" or y == "paper" or y == "scissors": # if both arguments are the same if x == y: return "Tie!" else: # let's play it! if x == "rock": if y == "paper": return 'Y won!' elif y == "scissors": return 'X won!' elif x == "paper": if y == "scissors": return 'Y won!' elif y == "rock": return 'X won!' elif x == "scissors": if y == "rock": return 'Y won!' elif y == "paper": return 'X won!' else: return "invalid value for y" else: return "invalid value for x" else: return "You must type only strings!"
 def scrabble_score(word): """ takes a string, compares its letters to score's indeces and computes points for each letter from string""" # table of scores score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1, "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, "x": 8, "z": 10} # let's make sure input string is lowercase word = word.lower() # init the total score totalScore = 0 # for each character in word for letter in word: # for each index ('a', 'b', 'c', 'd') in score dictionary for index in score: # if word's character = scores' index if letter == index: # storing points totalScore = totalScore + score[index] # that's it! return totalScore
 """Zeller's algorithm description: computes the day of the week on which a given date will fall (or fell) more info: http://en.wikipedia.org/wiki/Zeller's_congruence""" # input print "When were you born?" A = int(raw_input("Month of the year")) B = int(raw_input("Day of the month")) C = int(raw_input("Year of the century")) D = int(raw_input("Century")) # processing W = (13*A - 1) / 5 X = C / 4 Y = D / 4 Z = W + X + Y + B + C - 2*D R = Z % 7 # output if R > -1: if R == 0: print "You were born on Sunday" elif R == 1: print "You were born on Monday" elif R == 2: print "You were born on Tuesday" elif R == 3: print "You were born on Wednesday" elif R == 4: print "You were born on Thursday" elif R == 5: print "You were born on Friday" elif R == 6: print "You were born on Saturday" else: print "error 1" else: R = R + 7 if R == 0: print "You were born on Sunday" elif R == 1: print "You were born on Monday" elif R == 2: print "You were born on Tuesday" elif R == 3: print "You were born on Wednesday" elif R == 4: print "You were born on Thursday" elif R == 5: print "You were born on Friday" elif R == 6: print "You were born on Saturday" else: print "error 2"

### wv12 commented Jan 16, 2013

 Hi marioluan, I'm also a student from MOOC's Python course and was very interested to discover your answers to some of the exercises. I was surprised that your rock_paper_scissors game looks quite the same as mine. That's comforting. Just one remark on the dice_roll() function: r = random.randint(0, x) --> you should replace the "0" by "1" as for the randint() function both parameters are included. Though your die has x sides, you have x+1 possibilities (as 0 is included). greetings, wv12
Owner

### marioluan commented Jan 22, 2013

 Hi wv12, how are you doing with Python classes? Thanks for ponting me out on that, I've already made that change. cheers!!