Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions 1/assignment-week1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
print ("Week 1 Assignment - Math Equation Evaluator")
print ("Intern - Shu Wei")
print ("")

eArray = list(map(str, input("Please enter your input with spaces between them: ").split()))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to call str your input split will already be strings

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in python you probably dont need to call list. there are only a few cases you need to call list after a map. we might cover that later


#Definitions
aCount = (len(eArray))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

outer parens is useless

fCount = 3
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need more descriptive variable names.

nCount = 4
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python variable names are usually snake cases

snake_case_variable
camelCaseVariable
PascalCaseVariable

iDioTcAsEVariabLE

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question. If a variable is being called in a very frequent basis, wouldn't it be bad to have a long name since you'll need to type that long name every time?

needle = 0
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol ima assume you copied this code from somewhere

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

list(map(str, input("Please enter your input with spaces between them: ").split()))
Yes and I changed int to str :P


# Check whether the first two inputs are numbers and the last input is a sign
try:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: lets learn how to use functions to organize code

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we dont need to check for validity. for all assignments we'll assume well formed inputs

check1 = int(eArray[0])
except ValueError:
print("Please make sure that the input #1 is a number")
exit()
try:
check2 = int(eArray[1])
except ValueError:
print("Please make sure that the input #2 input is a number")
exit()

if aCount > 3:
CheckNCount = 4
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as pointed out above, python variable names are snake_cased. so this will be check_n_count (still not sure what it is tho)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check n count is just to check whether numbers are inputted at places where numbers are "allowed"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same thing, check s count is to check whether the operators are inputted at places where operators are allowed

while (CheckNCount <= aCount):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a while with only an upper bound is really easy to cause infinite loops. make sure to also set a lower bound just in case

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if anything, in general, you should prefer for over while , especially in python. in this case because your checkNCount changes value strangely, it might be hard to use for but usually that just makes it a "smell" (term we use for "code that smells fishy and might be bad")

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally, I tried to do it with "for" via:
checkNCount = 4, checkNCount < aCount, checkNCount++, but it didn't work

try:
checkN = int(eArray[CheckNCount-1])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since you were doing validation earlier on eArray you either converted its values to int already or you'd already failed it. so there's no point running int here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh so the conversion stays?

except ValueError:
print ("Please make sure that the input #", CheckNCount, "is a number")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read up on template strings (extra curricular)

exit()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exit are extremely bad practice. avoid at all cost

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should I use instead to make it stop running?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole thing should be in a function and you can return from the function. Or if you're expecting the validation to work, you can just throw an error and let the program crash

CheckNCount = CheckNCount + 2
else:
pass
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an else with pass is useless, you can remove both


if aCount >= 3:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see what you're doing. this is not good. Your code will only be able to handle 2 operations. What if you need to handle > 2 ? Part 2 of the assignment was hoping you'd arrive at a generic solution

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I coded it a way that it can handle n operations. What's stopping it from running more than 2 operations?

CheckSCount = 3
while (CheckSCount <= aCount):
try:
checkS = int(eArray[CheckSCount-1])
except ValueError:
if eArray[CheckSCount-1] == "+" or eArray[CheckSCount-1] == "-" or eArray[CheckSCount-1] == "*" or eArray[CheckSCount-1] == "/":
pass
else:
print("Invalid input #", CheckSCount)
else:
print ("Please make sure that the input #", CheckSCount, "is a sign")
exit ()
CheckSCount = CheckSCount + 2
else:
pass

#try:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always remove unused code

# check3 = int(eArray[aCount-1])
#except ValueError:
# if eArray[aCount-1] == "+" or eArray[aCount-1] == "-" or eArray[aCount-1] == "*" or eArray[aCount-1] == "/":
# pass
# else:
# print("Invalid last sign")
#else:
# print("Please make sure that the last input is a sign")
# exit()

# Calculation
import operator
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usually, in almost all languages, you'd import at the beginning. just a best of practice. Python lets you do it wherever, and there are cases where you'd have it inside some function but usually, imports at the top

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by the way, I have no idea what import does. I was trying to find a way to convert the text (+,-,*,/) into operations and this was one of the solution.

ops = {
"+" : operator.add,
"-" : operator.sub,
"*" : operator.mul,
"/" : operator.truediv,
}

result1 = ops[eArray[fCount-1]](int(eArray[0]), int(eArray[1]))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what makes aCount <= 4 special? you can totally fold this line into the while loop and avoid the if/else all together

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem that I saw is that in the array, item 1 and 2 are number, then afterwards, the order is even = number, odd = operator. I split the calculation in two parts because of this. It was originally <= 3, but for some reasons it doesn't work, and <=4 works.

if aCount > 4:
while (fCount < aCount):
fCount = fCount + 2
result1 = ops[eArray[fCount-1]](result1, int(eArray[nCount-1]))
nCount = nCount + 2
print (result1)
else:
print (result1)

14 changes: 14 additions & 0 deletions 1/fibonacci.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
number_of_characters = 20
stack = []
stack.append(int(0))
stack.append(int(1))

def fibonacci_function(number): #Je comprends toujours pas c'est quoi que je dois mettre dans le "()"; si je met rien ça marche pas; je peux mettre n'importe quoi et ça va marcher
current_stack_size = int(len(stack))
new_number = stack[current_stack_size - 1] + stack[current_stack_size - 2]
stack.append(int(new_number))

while len(stack) < number_of_characters:
fibonacci_function(number_of_characters)

print (stack)
45 changes: 45 additions & 0 deletions 1/hangman.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Problem: the game is currently case-sensitive. A != a
import getpass
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats getpass?


# Split the keyword into characters and put them into an array
def split(keyword):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usually there's no need to split a string into chars. it's already essentially a array of chars

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if anything i think list(keyword) will return an array of chars

return [char for char in keyword]

# Identify the number of guesses allowed
def minimum(answer):
if len(answer) < 5:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the whole thing can be max(len(answer) + 2, 5)

return 5
else:
return len(answer) + 2

# Check if the guessed letter matches something in the array, if so, replace the empty space in box
def check_answer(guess):
for index, letter in enumerate(answer):
if letter == guess:
box[index] = guess
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok this works but is a pattern we want to avoid.

either pass in box as an argument or dont use box at all. The reason is your function is now accessing a variable that's outside its scope and can easily create bugs


# Ask P1 to input a string for P2 to guess
keyword = getpass.getpass("Player 1: Please enter the word you want Player 2 to guess: ")
answer = split(keyword)
print ("Player 2: There are ",len(answer)," characters you need to guess")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use + instead of , . i think , prints extra spacing and looks weird


# Create a box to showcase P2 the empty places
box = []
while len(box) < len(answer):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can create box like so box = ["_" for i in range(len(answer))]

this means, you are creating box to be an array and you're initializing its values to be "_" for each position in a range of 0 to length of answer.

box.append("_")
print (box)

# Loop for the main game
number_of_guesses_left = int(minimum(answer))
while number_of_guesses_left > 0:
print ("You have ",number_of_guesses_left," chance left to guess. Please guess a character:")
guess = input()
check_answer(guess)
print (box)
if answer == box:
print ("Player 2 wins! The answer is: ", " ".join(answer))
quit()
else:
number_of_guesses_left = number_of_guesses_left - 1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can do number_of_guesses_left -= 1 does the same thing

else:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid using else with a while. it's a python only thing.

instead just do a check for if number_of_gueses_left <= 0 and player dies

print ("You ran out of guesses! Player 1 wins! The answer is: ", " ".join(answer))