Skip to content

Commit bc77ecd

Browse files
author
Mark Charlton
committed
most projects up to Wk8 at Wk6
1 parent e231718 commit bc77ecd

File tree

137 files changed

+5189
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+5189
-2
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# enter an integer and reverse it
2+
3+
# helpers
4+
5+
# integer reverser keeping to an int.
6+
def flip_me(i_num):
7+
i_flip=0
8+
while i_num != 0:
9+
i_last = i_num % 10
10+
i_flip = ( i_flip * 10 ) + i_last
11+
i_num = i_num // 10
12+
return i_flip
13+
14+
# inits
15+
s_err=""
16+
17+
# user input
18+
while True:
19+
try:
20+
tmp_in = int( input( f"{s_err}Enter a positive integer between 2 and 10 digits (-1 to exit): ") )
21+
# if the length matches, exit
22+
if tmp_in == int(-1) :
23+
break
24+
25+
if tmp_in > 9 and tmp_in < 10000000000 :
26+
print ( f'{str(tmp_in):10} reversed is {str(flip_me(tmp_in)):10}')
27+
s_err=""
28+
elif tmp_in < 0 :
29+
s_err = "Invalid value! "
30+
else :
31+
s_err = "Invalid length! "
32+
except ValueError:
33+
s_err="Invalid entry! "
34+
# except:
35+
# s_err="An unknown error occurred! "
36+
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
# kims game
2+
3+
# imports
4+
import random as r
5+
6+
# ESCAPE SEQUENCE CURSOR MOVEMENT
7+
# \033[<L>;<C>H Positions the cursor. Puts the cursor at line L and column C.
8+
# \033[<N>A Move the cursor up by N lines.
9+
# \033[<N>B Move the cursor down by N lines.
10+
# \033[<N>C Move the cursor forward by N columns.
11+
# \033[<N>D Move the cursor backward by N columns.
12+
# \033[2J Clear the screen, move to (0,0)
13+
# \033[K Erase the end of line.
14+
15+
UP = '\033[1A'
16+
CLEAR = '\x1b[2K'
17+
RESET = '\033[2J'
18+
BLINKON = '\033[32;5m'
19+
BLINKOFF = '\033[0m'
20+
#print ( UP, end=CLEAR)
21+
22+
# inits
23+
def build_grid():
24+
d_grid=[]
25+
for i in range(4):
26+
d_grid.append([{"val": "", "matched": 0, "check" : 0},
27+
{"val": "", "matched": 0, "check" : 0},
28+
{"val": "", "matched": 0, "check" : 0},
29+
{"val": "", "matched": 0, "check" : 0}])
30+
31+
l_cards=[]
32+
for i in range(4):
33+
l_cards.append("J")
34+
l_cards.append("Q")
35+
l_cards.append("K")
36+
l_cards.append("A")
37+
38+
r.shuffle(l_cards)
39+
40+
for i in range(4):
41+
for j in range(4):
42+
d_grid[i][j]["val"]=l_cards.pop()
43+
44+
return d_grid
45+
46+
def print_grid(d_grid, i_clear=0, i_reveal = 0 ):
47+
if i_clear == 1 :
48+
#print ( UP * 5, end=CLEAR)
49+
print (RESET)
50+
print ( "Kims Game v0.1")
51+
print ( "--------------------------")
52+
print ( "" )
53+
54+
print ( " " * 5 + " C 0 1 2 3 C" )
55+
print ( " " * 5 + "R |---------|" )
56+
57+
for x in range(4):
58+
print ( " " * 5 + str(x) + " |", end = " " )
59+
for y in range(4):
60+
if d_grid[x][y]["check"]==1 or i_reveal==1:
61+
print (d_grid[x][y]["val"], end=" ")
62+
elif d_grid[x][y]["matched"]==1:
63+
print ("X", end=" ")
64+
else:
65+
print ( "#", end = " " )
66+
print ("|")
67+
68+
print ( " " * 5 + "R |---------|" )
69+
print ("")
70+
71+
def get_location(d_grid):
72+
while True:
73+
try:
74+
loc1 = input ( "Please enter a location to flip (RC e.g 12) (ENTER to Quit) :" )
75+
if loc1=="":
76+
if check_exit() == "Y":
77+
break # exit function
78+
else:
79+
continue # go back and try again
80+
81+
if len(loc1) !=2:
82+
raise ValueError
83+
if int(loc1[0]) < 0 or int(loc1[0])> 3:
84+
raise ValueError
85+
86+
x1=int(loc1[0])
87+
y1=int(loc1[1])
88+
89+
if d_grid[x1][y1]["matched"]==1:
90+
print ( f"Location {loc1} is already matched " )
91+
raise Exception
92+
if d_grid[x1][y1]["check"]==1:
93+
print ( f"Location {loc1} is already flipped " )
94+
raise Exception
95+
else:
96+
break
97+
except ValueError:
98+
# Input failure
99+
print ("Invalid location entered")
100+
except:
101+
# it failed
102+
print ("Invalid location entered")
103+
return loc1
104+
105+
def check_exit():
106+
f_check=input("Are you sure you wish to quit? (Y/N) :").upper()
107+
return f_check
108+
109+
# more inits
110+
i_matches=0
111+
i_guesses=0
112+
d_grid=build_grid()
113+
114+
# Actually play the game
115+
while i_matches < 8:
116+
i_guesses += 1
117+
118+
print_grid ( d_grid, 1 )
119+
120+
loc1 = get_location(d_grid)
121+
122+
if loc1=="": # Exit condition
123+
break
124+
125+
x1=int(loc1[0])
126+
y1=int(loc1[1])
127+
d_grid[x1][y1]["check"]=1
128+
print_grid ( d_grid, 1 )
129+
130+
loc2 = get_location(d_grid)
131+
132+
if loc2=="": # Exit condition
133+
break
134+
135+
x2=int(loc2[0])
136+
y2=int(loc2[1])
137+
138+
d_grid[x2][y2]["check"]=1
139+
print_grid ( d_grid, 1 )
140+
141+
if d_grid[x2][y2]["val"] == d_grid[x1][y1]["val"]:
142+
d_grid[x1][y1]["matched"]=1
143+
d_grid[x2][y2]["matched"]=1
144+
i_matches += 1
145+
146+
# motivation
147+
print ( f"Great job. Match confirmed!", end=" ")
148+
if i_matches< 8:
149+
print ( f"Only {8-i_matches} pairs left." )
150+
else:
151+
print ( BLINKON + "You've won!" + BLINKOFF)
152+
else :
153+
# motivation
154+
print ( f"Keep trying, only {8-i_matches} pairs left." )
155+
156+
d_grid[x1][y1]["check"]=0
157+
d_grid[x2][y2]["check"]=0
158+
159+
x = input (BLINKON + "Press ENTER to continue" + BLINKOFF)
160+
161+
print_grid( d_grid, 1, 1)
162+
163+
if i_matches==8:
164+
print ( f"{BLINKON}Congratulations{BLINKOFF}. You cleared the board in {i_guesses} rounds! ")
165+
else:
166+
print ( f"Thanks for playing. You made {i_matches} matches in {i_guesses} rounds! ")
167+
168+
print ( "We hope to see you again soon." )
169+
print ( "" )
170+
171+
172+
173+
174+
175+
176+
177+
178+

.history/Assessment/Wk6/rainfall_20230310185143.py

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# display a chart of rainfall.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# display a chart of rainfall.
2+
3+
# imports
4+
import random as r
5+
6+
def create_chart():
7+
return 1
8+
9+
def gen_rand_data(d_rain):
10+
for key, value in d_rain.items():
11+
d_rain[key]=r.randrange(0,80)
12+
13+
d_rain = {"January": 0,
14+
"February": 0,
15+
"March": 0,
16+
"April": 0,
17+
"May": 0,
18+
"June": 0,
19+
"July": 0,
20+
"August": 0,
21+
"September": 0,
22+
"October": 0,
23+
"November": 0,
24+
"December": 0 }
25+
26+
d_rain = gen_rand_data( d_rain )
27+
28+
print ( d_rain )
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# display a chart of rainfall.
2+
3+
# imports
4+
import random as r
5+
6+
def create_chart():
7+
return 1
8+
9+
def gen_rand_data(d_rain):
10+
for key, value in d_rain.items():
11+
d_rain[key]=r.randrange(0,80)
12+
return d_rain
13+
14+
d_rain = {"January": 0,
15+
"February": 0,
16+
"March": 0,
17+
"April": 0,
18+
"May": 0,
19+
"June": 0,
20+
"July": 0,
21+
"August": 0,
22+
"September": 0,
23+
"October": 0,
24+
"November": 0,
25+
"December": 0 }
26+
27+
d_rain = gen_rand_data( d_rain )
28+
29+
print ( d_rain )
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# display a chart of rainfall.
2+
3+
# imports
4+
import random as r
5+
import matplotlib.pyplot as plt
6+
import numpy as np
7+
8+
def create_chart():
9+
return 1
10+
11+
def gen_rand_data(d_rain):
12+
for key, value in d_rain.items():
13+
d_rain[key]=r.randrange(0,80)
14+
return d_rain
15+
16+
def get_actual_data( d_rain ):
17+
for key, value in d_rain.items():
18+
d_rain[key]=int( input( f'Please enter the rainfall for {key}: ' ) )
19+
return d_rain
20+
21+
d_rain = {"January": 0,
22+
"February": 0,
23+
"March": 0,
24+
"April": 0,
25+
"May": 0,
26+
"June": 0,
27+
"July": 0,
28+
"August": 0,
29+
"September": 0,
30+
"October": 0,
31+
"November": 0,
32+
"December": 0 }
33+
34+
d_rain = gen_rand_data( d_rain )
35+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# display a chart of rainfall.
2+
3+
# imports
4+
import random as r
5+
import matplotlib.pyplot as plt
6+
import numpy as np
7+
8+
def create_chart():
9+
return 1
10+
11+
def gen_rand_data(d_rain):
12+
for key, value in d_rain.items():
13+
d_rain[key]=r.randrange(0,80)
14+
return d_rain
15+
16+
def get_actual_data( d_rain ):
17+
for key, value in d_rain.items():
18+
d_rain[key]=int( input( f'Please enter the rainfall for {key}: ' ) )
19+
return d_rain
20+
21+
d_rain = {"January": 0,
22+
"February": 0,
23+
"March": 0,
24+
"April": 0,
25+
"May": 0,
26+
"June": 0,
27+
"July": 0,
28+
"August": 0,
29+
"September": 0,
30+
"October": 0,
31+
"November": 0,
32+
"December": 0 }
33+
34+
## Generate test data for chart plotting practice.
35+
d_rain = gen_rand_data( d_rain )
36+
#d_rain = get_actual_data ( d_rain )
37+
38+
print ( d_rain )

0 commit comments

Comments
 (0)