User Tools

Site Tools


python:py_lists:py_code

This is an old revision of the document!


Python Code Examples

Title
Searchbcd
Sortingbcd
Stringsbcd
Mathbcd
Arraybcd
Linked Listbcd
Stacksbcd
Queuesbcd
Hash Tablesbcd
Binary Treebcd
Binary Heapbcd
Graphsbcd
yyybcd
yyybcd
abcd
abcd
abcd
Build a Deck of 52 Cards
import random

suits = ["Clubs", "Diamonds", "Hearts", "Spades"]
faces = ["Jack", "Queen", "King", "Ace"]
numbered = [2, 3, 4, 5, 6, 7, 8, 9, 10] 

deck = set()

for suit in suits:
    for card in faces + numbered:
        deck.add((card, "of", suit))

def draw ():
    card = random.choice(list(deck))
    deck.remove(card)
    return card

card = ("Ace", "of", "Hearts")
card in deck

print(draw())

if card in deck:
    print("that card is available")
else:
    print("that card already taken")
    

ml = "Wolf,Knoll,Orc,Troll,Dragon"
monster1, monster2, monster3, monster4, monster5 = ml.split(",")
Swim Code
import statistics

FN = "Darius-13-100m-Fly.txt"

FOLDER = "swimdata/"

with open(FOLDER + FN) as file:
    lines = file.readlines()

times = lines[0].strip().split(",")

converts =[]
for t in times:
    minutes, rest = t.split(":")
    seconds, hundredths = rest.split(".")
    converts.append((int(minutes) * 60  * 100) + (int(seconds) * 100) + int(hundredths))

average = statistics.mean(converts)
average / 100
round(average / 100, 2)

min_secs, hundredths = str(round(average /100, 2)).split(".")
min_secs = int(min_secs)
minutes = min_secs // 60
minutes

seconds = min_secs - minutes*60
seconds

average = str(minutes) + ":" + str(seconds) + "." + hundredths
python/py_lists/py_code.1729441368.txt.gz · Last modified: by adminguide