Skip to content

Instantly share code, notes, and snippets.

@kkt-ee
kkt-ee / printRandomIntegers.py
Created January 1, 2020 15:12
Printing random integers in between 1 and 10 random.randint(1,10)
import random
for i in range(5):
print(random.randint(1, 10))
@kkt-ee
kkt-ee / exit_flowControl.py
Created January 1, 2020 15:14
demo flow control: exit program with sys.exit()
import sys
while True:
print('Type exit to exit.')
response = input()
if response == 'exit':
sys.exit()
print('You typed '+response+'.')
@kkt-ee
kkt-ee / coinTossGame.py
Created January 1, 2020 16:12
A demo coin toss game with random.randint(0,1) where 0,1 => tail, head respectively
import random
guess = ''
count = 0
flag = True
while guess not in ('h', 't') and flag == True:
guess = input('\nGuess the coin toss! Enter heads or tails:')
guess = 1 if guess == 'h' else 0
toss = random.randint(0, 1) # 0 is tails, 1 is heads
@kkt-ee
kkt-ee / pdfRead.py
Created January 1, 2020 17:06
read pdf file with PyPDF2
#pdf open/read
#parse
import PyPDF2 as pdf
def pdfRead(file):
pdfobj = open(file, 'rb')
pdfread = pdf.PdfFileReader(pdfobj)
totpages = pdfread.numPages
@kkt-ee
kkt-ee / spin_words.py
Created January 2, 2020 14:33
Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed (Just like the name of this Kata). Strings passed in will consist of only letters and spaces. Spaces will be included only when more than one word is present. Examples: spinWords( "Hey fellow warriors" ) => r…
def spin_words(sentence):
# Your code goes here
words = sentence.split(' ')#[::-1]
newL = list()
for word in words:
newL.append(word) if len(word)<5 else newL.append(word[::-1])
return ' '.join(newL)
#'sroirraw wollef Hey' should equal 'Hey wollef sroirraw'
def spin_words(sentence):
# Your code goes here
words = sentence.split(' ')[::-1]
newL = list()
for word in words:
newL.append(word) if len(word)<5 else newL.append(word[::-1])
return ' '.join(newL)
@kkt-ee
kkt-ee / disemvowel.py
Created January 4, 2020 16:57
remove vowels
#remove vowels
def disemvowel(s):
return s.translate(None, "aeiouAEIOU")
def disemvowel(string):
return "".join(c for c in string if c.lower() not in "aeiou")
"""
ATM machines allow 4 or 6 digit PIN codes and PIN codes cannot contain anything but exactly 4 digits or exactly 6 digits.
If the function is passed a valid PIN string, return true, else return false.
"""
def validate_pin(pin):
#return true or false
return pin.isdigit() and (len(pin) == 4 or len(pin) == 6)
#OR
"""
Simple, given a string of words, return the length of the shortest word(s).
String will never be empty and you do not need to account for different data types.
"""
def find_short(s):
# your code here
l = min([len(x) for x in s.split()])
return l # l: shortest word length
"""
Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result.
It should remove all values from list a, which are present in list b.
"""
def array_diff(a, b):
#your code here
return [x for x in a if x not in b]