Skip to content

Instantly share code, notes, and snippets.

@kstampo
Last active October 22, 2017 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kstampo/51911b4e00fc2aef7ec52f43df469f5f to your computer and use it in GitHub Desktop.
Save kstampo/51911b4e00fc2aef7ec52f43df469f5f to your computer and use it in GitHub Desktop.
Automate the boring stuff with Python
def collatz(number):
if number%2==0:
#print (number//2)
return number//2
else:
#print (3*number+1)
return 3*number+1
try:
print ("Enter a number", end=" ")
num = int(input())
while collatz(num) != 1:
print(collatz(num))
num = collatz(num)
except ValueError:
print ("Please enter a number")
def rows2cols(lista):
originalRows=len(lista)
originalCols=len(lista[0])
newlista = []
fulllist=[]
for i in range(0,originalCols): #0-5
del (newlista)
newlista = []
for j in range(originalRows-1,-1,-1): #9-0
newlista.append(lista[j][i])
#print(newlista)
fulllist.append(newlista)
#print (fulllist)
#convert list fulllist to string
retstring=""
for i in range(0,len(fulllist)):
for j in range (0,len(fulllist[0])):
retstring+=fulllist[i][j]
retstring+="\n"
return retstring
grid = [['.', '.', '.', '.', '.', '.'],
['.', 'O', 'O', '.', '.', '.'],
['O', 'O', 'O', 'O', '.', '.'],
['O', 'O', 'O', 'O', 'O', '.'],
['.', 'O', 'O', 'O', 'O', 'O'],
['O', 'O', 'O', 'O', 'O', '.'],
['O', 'O', 'O', 'O', '.', '.'],
['.', 'O', 'O', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]
print (grid)
print("\n"*3)
print (rows2cols(grid))
def func(spam):
protasi=""
for i in range(0,len(spam)-2):
protasi+=spam[i]+", "
protasi+=spam[-2]+" and " +spam[-1]
return protasi
print (func(['apples', 'bananas', 'tofu', 'cats']))
print (func(['just', 'me', 'myself', 'I']))
import pprint
birthdays={'Kostas':'30 Mar', 'Anastasia':'22 Jan'}
while True:
print ("Δώσε το όνομα για να σου πω τα γενέθλια, ή πάτα ENTER για τερματισμό ", end="")
onoma=input()
if onoma=="":
break
if onoma in birthdays:
print("Τα γενέθλια του/της "+onoma+" είναι: " + birthdays[onoma])
else:
print ("Δεν έχω στη Βάση τα γενέθλια του/της "+onoma+ ". Δώσε τώρα την ημερομηνία:", end="")
hmnia=input()
birthdays[onoma]=hmnia
print(birthdays)
for k in birthdays.keys():
protasi= ("Ο/Η " + k + " γεννήθηκε την " + birthdays[k])
print (protasi)
count={}
for k in protasi:
count.setdefault(k,0)
count[k]+=1
pprint.pprint(count)
# inventory.py
def displayInventory(inventory):
print("Inventory:")
item_total = 0
for k, v in inventory.items():
print(str(v) + ' ' + k)
item_total += v
print("Total number of items: " + str(item_total))
def addToInventory(inventory, addedItems):
for antikeimena in addedItems:
inventory.setdefault(antikeimena,0)
for items,posotites in inventory.items():
if antikeimena==items:
inventory[items]+=1
return inventory
#stuff = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
#displayInventory(stuff)
inv = {'gold coin': 42, 'rope': 1}
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
inv = addToInventory(inv, dragonLoot)
displayInventory(inv)
allGuests = {'Alice': {'apples': 5, 'pretzels': 12},'Bob': {'ham sandwiches': 3, 'apples': 2},'Carol': {'cups': 3, 'apple pies': 1}}
def totalBrought(guests, item):
numBrought = 0
for k, v in guests.items():
numBrought += v.get(item, 0)
return numBrought
print('Number of things being brought:')
print(' - Apples ' + str(totalBrought(allGuests, 'apples')))
print(' - Cups ' + str(totalBrought(allGuests, 'cups')))
print(' - Cakes ' + str(totalBrought(allGuests, 'cakes')))
print(' - Ham Sandwiches ' + str(totalBrought(allGuests, 'ham sandwiches')))
print(' - Apple Pies ' + str(totalBrought(allGuests, 'apple pies')))
import random
def drawBoard(board):
print (board[1]+" | " + board[2]+" | "+ board[3])
print ("---------")
print (board[4]+" | " + board[5]+" | "+ board[6])
print ("---------")
print (board[7]+" | " + board[8]+" | "+ board[9])
def winner(board):
wins=[(1,2,3),(4,5,6),(7,8,9),(1,5,9),(3,5,7),(1,4,7),(2,5,8),(3,6,9)]
for i in range(0,len(wins)):
if board[wins[i][0]]=="X" and board[wins[i][1]]=="X" and board[wins[i][2]]=="X":
return ("Human Wins!!!")
if board[wins[i][0]]=="O" and board[wins[i][1]]=="O" and board[wins[i][2]]=="O":
return ("Computer Wins!!!")
def fullboard(board):
if "." in board.values():
return True
theBoard={1:'.',2:'.',3:'.',4:'.',5:'.',6:'.',7:'.',8:'.',9:'.'}
drawBoard(theBoard)
print ("\n"*5)
while True:
if not fullboard(theBoard):
print ("Τέλος παιχνιδιου!")
break
print("Give me the position for placing X (1-9) ", end="")
pos=int(input())
theBoard[pos]="X"
print ("\n"*8)
drawBoard(theBoard)
if winner(theBoard):
print(winner(theBoard))
break
if not fullboard(theBoard):
print ("Τέλος παιχνιδιου!")
break
print ("Press enter for me to play....")
input()
comppos=random.randint(1,9)
while True:
if theBoard[comppos]!="X" and theBoard[comppos]!="O":
theBoard[comppos] = "O"
break
else:
comppos = random.randint(1, 9)
print ("\n"*8)
drawBoard(theBoard)
if winner(theBoard):
print(winner(theBoard))
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment