Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
210-CT/Week 1.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
54 lines (33 sloc)
1.66 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Week 1 Tasks | |
#Task 1 | |
def randomRearrange(yourList): | |
"""This function will randomly shuffle the elements in a list""" | |
import random | |
for i in yourList: | |
oldPos = yourList.index(i) #get the original position of the element in the list | |
newPos = random.randint(0,(len(yourList)-1)) #get the new random position that the element will be moved to | |
yourList[oldPos], yourList[newPos] = yourList[newPos], yourList[oldPos] #swapping the elements position | |
return(yourList) | |
#getting the user input through a loop so they can enter each element in their list individually | |
listRange = int(input("How many elements are in your list? >> ")) | |
listOfInts = [] | |
for i in range(0,listRange): | |
x = int(input("Enter element >> ")) | |
listOfInts.append(x) | |
print(randomRearrange(listOfInts)) | |
#Task 2 basiz | |
def TrailingZeros(factorial): | |
""" Given a number (factorial) this function will count the number of trailing zeros""" | |
import math | |
#x is the number I will divide the factorial by, it will start at ten and the be multiplied by ten in each iteration | |
#n is the number of trailing zeros. n has to start as negative one as it will run the while loop one more time adding an extra one before it know to terminate. | |
#a is a variable that will cut off the while loop when it drops below 1, meaning that the factorial isn't devisable by the multiple of ten | |
Fnumber = int(math.factorial(factorial)) | |
x = 10 | |
n = -1 | |
a = 0 | |
while a == 0: | |
a = Fnumber%x #if the remainder is 0 than the factorial is a multiple of ten and has a trailing zero | |
x = x*10 | |
n = n + 1 | |
return(n) | |