Skip to content

Commit b344523

Browse files
committed
Merge remote-tracking branch 'geekComputers/master'
2 parents e838414 + 2c69b35 commit b344523

31 files changed

+935
-76
lines changed

4 Digit Number Combinations.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,10 @@ def FourDigitCombinations():
33
numbers=[]
44
for code in range(10000):
55
code=str(code).zfill(4)
6-
print code,
6+
print(code)
77
numbers.append(code)
8+
9+
# Same as above but more pythonic
10+
def oneLineCombinations():
11+
numbers = list(map(lambda x: str(x).zfill(4), [i for i in range(10000)]))
12+
print(numbers)

EncryptionTool.py

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#GGearing
2+
#Simple encryption script for text
3+
#This was one my first versions of this script
4+
#09/07/2017
5+
6+
text=input("Enter text: ")
7+
PI=3.14159265358979323846264338327950288419716939937510
8+
text=list(text)
9+
values= list()
10+
reverse=list()
11+
def encryptChar(target):
12+
#encrytion algorithm
13+
target=(((target+42)*PI)-449)
14+
return target
15+
16+
def decryptChar(target):
17+
target=(((target+449)/PI)-42)
18+
return target
19+
20+
def encrypt(input_text):
21+
input_text_list=list(input_text)
22+
col_values=list()
23+
for i in range (len(input_text_list)):
24+
current=ord(input_text_list[i])
25+
current=encryptChar(current)
26+
col_values.append(current)
27+
return col_values
28+
29+
def decrypt(enc_text):
30+
enc_list
31+
for i in range (len(input_text_list)):
32+
current=int(decryptChar(values[i]))
33+
current=chr(current)
34+
col_values.append(current)
35+
return col_values
36+
37+
def readAndDecrypt(filename):
38+
file=open(filename,"r")
39+
data=file.read()
40+
datalist=list()
41+
datalistint=list()
42+
actualdata=list()
43+
datalist=data.split(" ")
44+
datalist.remove('')
45+
for i in range(len(datalist)):
46+
datalistint.append(float(datalist[i]))
47+
for i in range(len(datalist)):
48+
current1=int(decryptChar(datalistint[i]))
49+
current1=chr(current1)
50+
actualdata.append(current1)
51+
return actualdata
52+
53+
def readAndEncrypt(filename):
54+
file=open(filename,"r")
55+
data=file.read()
56+
datalist=list(data)
57+
encrypted_list=list()
58+
encrypted_list_str=list()
59+
for i in range(len(datalist)):
60+
current=ord(datalist[i])
61+
current=encryptChar(current)
62+
encrypted_list.append(current)
63+
return encrypted_list
64+
65+
def readAndEncryptAndSave(inp_file,out_file):
66+
enc_list=readAndEncrypt(inp_file)
67+
output=open(out_file,"w")
68+
for i in range(len(enc_list)):
69+
output.write(str(enc_list[i])+" ")
70+
output.close()
71+
72+
def readAndDecryptAndSave(inp_file,out_file):
73+
dec_list=readAndDecrypt(inp_file)
74+
output=open(out_file,"w")
75+
for i in range(len(dec_list)):
76+
output.write(str(dec_list[i]))
77+
output.close()
78+
#encryption
79+
for i in range (len(text)):
80+
current=ord(text[i])
81+
current=encryptChar(current)
82+
values.append(current)
83+
84+
#decryption
85+
for i in range (len(text)):
86+
current=int(decryptChar(values[i]))
87+
current=chr(current)
88+
reverse.append(current)
89+
print(reverse)
90+
91+
#saves encrypted in txt file
92+
output=open("encrypted.txt","w")
93+
for i in range(len(values)):
94+
output.write(str(values[i])+" ")
95+
output.close()
96+
97+
#read and decrypts
98+
print(readAndDecrypt("encrypted.txt"))
99+
100+

Print_List_of_Even_Numbers.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
print [x for x in range(2, 100) if not x % 2]
2-
print range(2, 100, 2)
1+
#user can give in put now
2+
#pyhton3
3+
4+
print ([x for x in range(int(input()),int(input())) if not x%2])

QuadraticCalc.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#GGearing
2+
#02/10/2017
3+
#Simple script to calculate the quadratic formula of a sequence of numbers and
4+
#recognises when the sequence isn't quadratic
5+
6+
def findLinear(numbers): ##find a & b of linear sequence
7+
output=[]
8+
a=numbers[1]-numbers[0]
9+
a1=numbers[2]-numbers[1]
10+
if a1==a:
11+
b=numbers[0]-a
12+
return (a,b)
13+
else:
14+
print("Sequence is not linear")
15+
16+
sequence=[]
17+
first_difference=[]
18+
second_difference=[]
19+
for i in range(4): #input
20+
term=str(i+1)
21+
inp=int(input("Enter term "+term+": "))
22+
sequence.append(inp)
23+
24+
for i in range(3):
25+
gradient=sequence[i+1]-sequence[i]
26+
first_difference.append(gradient)
27+
for i in range(2):
28+
gradient=first_difference[i+1]-first_difference[i]
29+
second_difference.append(gradient)
30+
31+
if second_difference[0]==second_difference[1]: #checks to see if consistent
32+
a=second_difference[0]/2
33+
subs_diff=[]
34+
for i in range(4):
35+
n=i+1
36+
num=a*(n*n)
37+
subs_diff.append((sequence[i])-num)
38+
b,c=findLinear(subs_diff)
39+
print("Nth term: "+str(a)+"n^2 + "+str(b)+"n + "+str(c)) #outputs nth term
40+
else:
41+
print("Sequence is not quadratic")
42+

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,5 @@ In the scripts the comments etc are lined up correctly when they are viewed in [
6060
- `Google_News.py` - Uses BeautifulSoup to provide Latest News Headline along with news link.
6161

6262
- `cricket_live_score` - Uses BeautifulSoup to provide live cricket score.
63+
64+
- `youtube.py` - Takes input a song name and fetches the youtube url of best matching song and plays it.

SimpleStopWatch.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
print('Press ENTER to begin, Press Ctrl + C to stop')
77
while True:
88
try:
9-
input() #For ENTER
9+
input() # For ENTER. Use raw_input() if you are running python 2.x instead of input()
1010
starttime = time.time()
1111
print('Started')
1212
except KeyboardInterrupt:

TicTacToe.py

+182
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
# Tic Tac Toe
2+
3+
import random
4+
5+
def drawBoard(board):
6+
# This function prints out the board that it was passed.
7+
8+
# "board" is a list of 10 strings representing the board (ignore index 0)
9+
print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])
10+
print('-----------')
11+
print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])
12+
print('-----------')
13+
print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])
14+
15+
def inputPlayerLetter():
16+
# Lets the player type which letter they want to be.
17+
# Returns a list with the player's letter as the first item, and the computer's letter as the second.
18+
letter = ''
19+
while not (letter == 'X' or letter == 'O'):
20+
print('Do you want to be X or O?')
21+
letter = input().upper()
22+
23+
# the first element in the tuple is the player's letter, the second is the computer's letter.
24+
if letter == 'X':
25+
return ['X', 'O']
26+
else:
27+
return ['O', 'X']
28+
29+
def whoGoesFirst():
30+
# Randomly choose the player who goes first.
31+
if random.randint(0, 1) == 0:
32+
return 'computer'
33+
else:
34+
return 'player'
35+
36+
def playAgain():
37+
# This function returns True if the player wants to play again, otherwise it returns False.
38+
print('Do you want to play again? (yes or no)')
39+
return input().lower().startswith('y')
40+
41+
def makeMove(board, letter, move):
42+
board[move] = letter
43+
44+
def isWinner(bo, le):
45+
# Given a board and a player's letter, this function returns True if that player has won.
46+
# We use bo instead of board and le instead of letter so we don't have to type as much.
47+
return ((bo[7] == le and bo[8] == le and bo[9] == le) or # across the top
48+
(bo[4] == le and bo[5] == le and bo[6] == le) or # across the middle
49+
(bo[1] == le and bo[2] == le and bo[3] == le) or # across the bottom
50+
(bo[7] == le and bo[4] == le and bo[1] == le) or # down the left side
51+
(bo[8] == le and bo[5] == le and bo[2] == le) or # down the middle
52+
(bo[9] == le and bo[6] == le and bo[3] == le) or # down the right side
53+
(bo[7] == le and bo[5] == le and bo[3] == le) or # diagonal
54+
(bo[9] == le and bo[5] == le and bo[1] == le)) # diagonal
55+
56+
def getBoardCopy(board):
57+
# Make a duplicate of the board list and return it the duplicate.
58+
dupeBoard = []
59+
60+
for i in board:
61+
dupeBoard.append(i)
62+
63+
return dupeBoard
64+
65+
def isSpaceFree(board, move):
66+
# Return true if the passed move is free on the passed board.
67+
return board[move] == ' '
68+
69+
def getPlayerMove(board):
70+
# Let the player type in his move.
71+
move = ' '
72+
while move not in '1 2 3 4 5 6 7 8 9'.split() or not isSpaceFree(board, int(move)):
73+
print('What is your next move? (1-9)')
74+
move = input()
75+
return int(move)
76+
77+
def chooseRandomMoveFromList(board, movesList):
78+
# Returns a valid move from the passed list on the passed board.
79+
# Returns None if there is no valid move.
80+
possibleMoves = []
81+
for i in movesList:
82+
if isSpaceFree(board, i):
83+
possibleMoves.append(i)
84+
85+
if len(possibleMoves) != 0:
86+
return random.choice(possibleMoves)
87+
else:
88+
return None
89+
90+
def getComputerMove(board, computerLetter):
91+
# Given a board and the computer's letter, determine where to move and return that move.
92+
if computerLetter == 'X':
93+
playerLetter = 'O'
94+
else:
95+
playerLetter = 'X'
96+
97+
# Here is our algorithm for our Tic Tac Toe AI:
98+
# First, check if we can win in the next move
99+
for i in range(1, 10):
100+
copy = getBoardCopy(board)
101+
if isSpaceFree(copy, i):
102+
makeMove(copy, computerLetter, i)
103+
if isWinner(copy, computerLetter):
104+
return i
105+
106+
# Check if the player could win on his next move, and block them.
107+
for i in range(1, 10):
108+
copy = getBoardCopy(board)
109+
if isSpaceFree(copy, i):
110+
makeMove(copy, playerLetter, i)
111+
if isWinner(copy, playerLetter):
112+
return i
113+
114+
# Try to take one of the corners, if they are free.
115+
move = chooseRandomMoveFromList(board, [1, 3, 7, 9])
116+
if move != None:
117+
return move
118+
119+
# Try to take the center, if it is free.
120+
if isSpaceFree(board, 5):
121+
return 5
122+
123+
# Move on one of the sides.
124+
return chooseRandomMoveFromList(board, [2, 4, 6, 8])
125+
126+
def isBoardFull(board):
127+
# Return True if every space on the board has been taken. Otherwise return False.
128+
for i in range(1, 10):
129+
if isSpaceFree(board, i):
130+
return False
131+
return True
132+
133+
134+
print('Welcome to Tic Tac Toe!')
135+
136+
while True:
137+
# Reset the board
138+
theBoard = [' '] * 10
139+
playerLetter, computerLetter = inputPlayerLetter()
140+
turn = whoGoesFirst()
141+
print('The ' + turn + ' will go first.')
142+
gameIsPlaying = True
143+
144+
while gameIsPlaying:
145+
if turn == 'player':
146+
# Player's turn.
147+
drawBoard(theBoard)
148+
move = getPlayerMove(theBoard)
149+
makeMove(theBoard, playerLetter, move)
150+
151+
if isWinner(theBoard, playerLetter):
152+
drawBoard(theBoard)
153+
print('Hooray! You have won the game!')
154+
gameIsPlaying = False
155+
else:
156+
if isBoardFull(theBoard):
157+
drawBoard(theBoard)
158+
print('The game is a tie!')
159+
break
160+
else:
161+
turn = 'computer'
162+
163+
else:
164+
# Computer's turn.
165+
move = getComputerMove(theBoard, computerLetter)
166+
makeMove(theBoard, computerLetter, move)
167+
168+
if isWinner(theBoard, computerLetter):
169+
drawBoard(theBoard)
170+
print('The computer has beaten you! You lose.')
171+
gameIsPlaying = False
172+
else:
173+
if isBoardFull(theBoard):
174+
drawBoard(theBoard)
175+
print('The game is a tie!')
176+
break
177+
else:
178+
turn = 'player'
179+
180+
if not playAgain():
181+
break
182+

WikipediaModule

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ def fullPage(page):
4646
return soup
4747

4848
def randomWiki():
49-
'''
50-
This function gives you a list of n number of random articles
51-
Choose any article.
52-
'''
49+
'''
50+
This function gives you a list of n number of random articles
51+
Choose any article.
52+
'''
5353
number=input("No: of Random Pages : ")
5454
lst=wk.random(number)
5555
for i in enumerate(lst):

0 commit comments

Comments
 (0)