mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-01 00:41:09 +00:00
Merge pull request #242 from cclauss/modernize-python2-code-again
Modernize Python 2 code to get ready for Python 3 AGAIN
This commit is contained in:
commit
bc34f6e18f
|
@ -8,21 +8,21 @@ host = socket.gethostname() # Get local machine name
|
||||||
s.bind((host, port)) # Bind to the port
|
s.bind((host, port)) # Bind to the port
|
||||||
s.listen(5) # Now wait for client connection.
|
s.listen(5) # Now wait for client connection.
|
||||||
|
|
||||||
print 'Server listening....'
|
print('Server listening....')
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
conn, addr = s.accept() # Establish connection with client.
|
conn, addr = s.accept() # Establish connection with client.
|
||||||
print 'Got connection from', addr
|
print('Got connection from', addr)
|
||||||
data = conn.recv(1024)
|
data = conn.recv(1024)
|
||||||
print('Server received', repr(data))
|
print('Server received', repr(data))
|
||||||
|
|
||||||
filename = 'mytext.txt'
|
filename = 'mytext.txt'
|
||||||
f = open(filename, 'rb')
|
f = open(filename, 'rb')
|
||||||
l = f.read(1024)
|
in_data = f.read(1024)
|
||||||
while (l):
|
while (in_data):
|
||||||
conn.send(l)
|
conn.send(in_data)
|
||||||
print('Sent ',repr(l))
|
print('Sent ', repr(in_data))
|
||||||
l = f.read(1024)
|
in_data = f.read(1024)
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
print('Done sending')
|
print('Done sending')
|
||||||
|
@ -42,7 +42,7 @@ s.connect((host, port))
|
||||||
s.send("Hello server!")
|
s.send("Hello server!")
|
||||||
|
|
||||||
with open('received_file', 'wb') as f:
|
with open('received_file', 'wb') as f:
|
||||||
print 'file opened'
|
print('file opened')
|
||||||
while True:
|
while True:
|
||||||
print('receiving data...')
|
print('receiving data...')
|
||||||
data = s.recv(1024)
|
data = s.recv(1024)
|
||||||
|
|
|
@ -6,11 +6,13 @@ def main():
|
||||||
32=9, 33=27, 34=81, 35=243
|
32=9, 33=27, 34=81, 35=243
|
||||||
42=16, 43=64, 44=256, 45=1024
|
42=16, 43=64, 44=256, 45=1024
|
||||||
52=25, 53=125, 54=625, 55=3125
|
52=25, 53=125, 54=625, 55=3125
|
||||||
If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:
|
If they are then placed in numerical order, with any repeats removed,
|
||||||
|
we get the following sequence of 15 distinct terms:
|
||||||
|
|
||||||
4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125
|
4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125
|
||||||
|
|
||||||
How many distinct terms are in the sequence generated by ab for 2 <= a <= 100 and 2 <= b <= 100?
|
How many distinct terms are in the sequence generated by ab
|
||||||
|
for 2 <= a <= 100 and 2 <= b <= 100?
|
||||||
"""
|
"""
|
||||||
|
|
||||||
collectPowers = set()
|
collectPowers = set()
|
||||||
|
@ -20,14 +22,11 @@ def main():
|
||||||
N = 101 # maximum limit
|
N = 101 # maximum limit
|
||||||
|
|
||||||
for a in range(2, N):
|
for a in range(2, N):
|
||||||
|
|
||||||
for b in range(2, N):
|
for b in range(2, N):
|
||||||
|
|
||||||
currentPow = a**b # calculates the current power
|
currentPow = a**b # calculates the current power
|
||||||
collectPowers.add(currentPow) # adds the result to the set
|
collectPowers.add(currentPow) # adds the result to the set
|
||||||
|
|
||||||
|
print("Number of terms ", len(collectPowers))
|
||||||
print "Number of terms ", len(collectPowers)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -12,7 +12,7 @@ class TrieNode:
|
||||||
self.nodes = dict() # Mapping from char to TrieNode
|
self.nodes = dict() # Mapping from char to TrieNode
|
||||||
self.is_leaf = False
|
self.is_leaf = False
|
||||||
|
|
||||||
def insert_many(self, words: [str]): # noqa: F821 This syntax is Python 3 only
|
def insert_many(self, words: [str]): # noqa: E999 This syntax is Python 3 only
|
||||||
"""
|
"""
|
||||||
Inserts a list of words into the Trie
|
Inserts a list of words into the Trie
|
||||||
:param words: list of string words
|
:param words: list of string words
|
||||||
|
@ -21,7 +21,7 @@ class TrieNode:
|
||||||
for word in words:
|
for word in words:
|
||||||
self.insert(word)
|
self.insert(word)
|
||||||
|
|
||||||
def insert(self, word: str): # noqa: F821 This syntax is Python 3 only
|
def insert(self, word: str): # noqa: E999 This syntax is Python 3 only
|
||||||
"""
|
"""
|
||||||
Inserts a word into the Trie
|
Inserts a word into the Trie
|
||||||
:param word: word to be inserted
|
:param word: word to be inserted
|
||||||
|
@ -34,7 +34,7 @@ class TrieNode:
|
||||||
curr = curr.nodes[char]
|
curr = curr.nodes[char]
|
||||||
curr.is_leaf = True
|
curr.is_leaf = True
|
||||||
|
|
||||||
def find(self, word: str) -> bool: # noqa: F821 This syntax is Python 3 only
|
def find(self, word: str) -> bool: # noqa: E999 This syntax is Python 3 only
|
||||||
"""
|
"""
|
||||||
Tries to find word in a Trie
|
Tries to find word in a Trie
|
||||||
:param word: word to look for
|
:param word: word to look for
|
||||||
|
@ -48,7 +48,7 @@ class TrieNode:
|
||||||
return curr.is_leaf
|
return curr.is_leaf
|
||||||
|
|
||||||
|
|
||||||
def print_words(node: TrieNode, word: str): # noqa: F821 This syntax is Python 3 only
|
def print_words(node: TrieNode, word: str): # noqa: E999 This syntax is Python 3 only
|
||||||
"""
|
"""
|
||||||
Prints all the words in a Trie
|
Prints all the words in a Trie
|
||||||
:param node: root node of Trie
|
:param node: root node of Trie
|
||||||
|
|
|
@ -10,6 +10,8 @@ Example:
|
||||||
a=daBcd and b="ABC"
|
a=daBcd and b="ABC"
|
||||||
daBcd -> capitalize a and c(dABCd) -> remove d (ABC)
|
daBcd -> capitalize a and c(dABCd) -> remove d (ABC)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def abbr(a, b):
|
def abbr(a, b):
|
||||||
n = len(a)
|
n = len(a)
|
||||||
m = len(b)
|
m = len(b)
|
||||||
|
@ -26,4 +28,4 @@ def abbr(a, b):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print abbr("daBcd", "ABC") # expect True
|
print(abbr("daBcd", "ABC")) # expect True
|
||||||
|
|
|
@ -7,14 +7,14 @@ import sys
|
||||||
|
|
||||||
|
|
||||||
# returns F(n)
|
# returns F(n)
|
||||||
def fibonacci(n: int): # noqa: F821 This syntax is Python 3 only
|
def fibonacci(n: int): # noqa: E999 This syntax is Python 3 only
|
||||||
if n < 0:
|
if n < 0:
|
||||||
raise ValueError("Negative arguments are not supported")
|
raise ValueError("Negative arguments are not supported")
|
||||||
return _fib(n)[0]
|
return _fib(n)[0]
|
||||||
|
|
||||||
|
|
||||||
# returns (F(n), F(n-1))
|
# returns (F(n), F(n-1))
|
||||||
def _fib(n: int): # noqa: F821 This syntax is Python 3 only
|
def _fib(n: int): # noqa: E999 This syntax is Python 3 only
|
||||||
if n == 0:
|
if n == 0:
|
||||||
# (F(0), F(1))
|
# (F(0), F(1))
|
||||||
return (0, 1)
|
return (0, 1)
|
||||||
|
|
|
@ -70,8 +70,8 @@ def mbd(predict, actual):
|
||||||
difference = predict - actual
|
difference = predict - actual
|
||||||
numerator = np.sum(difference) / len(predict)
|
numerator = np.sum(difference) / len(predict)
|
||||||
denumerator = np.sum(actual) / len(predict)
|
denumerator = np.sum(actual) / len(predict)
|
||||||
print str(numerator)
|
print(numerator)
|
||||||
print str(denumerator)
|
print(denumerator)
|
||||||
|
|
||||||
score = float(numerator) / denumerator * 100
|
score = float(numerator) / denumerator * 100
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user