N.rstrip()
does not modify a string in place and returns a new string instead. Since you are not assigning the result to anything, it does nothing- according to PEP8, you should put underscores between the words in variable and function names -
number_of_tries
vsnumberoftries
ordigitlist
vsdigit_list
,get_number()
vsgetnumber()
- following the previous note -
N
is not a good variable name - remove unused import
count = count + 1
can be rewritten ascount += 1
- put the main logic of the program into
if __name__ == '__main__':
if __name__ == '__main__':
- use
print()
function instead of a statement for Python 3.x compatibility - use
with
context manager when opening file(s) - this will ensure it being properly and safely closed - use
os.path.join()
to join the path components (okay, you now need to get yourimport os
back :) - define constants in upper case (PEP8 reference)
- use list comprehensions
- use
.format()
instead of string concatenation - use built-in
sum()
function to for counting - loop over the items directly instead of indexes
N.rstrip()
does not modify a string in place and returns a new string instead. Since you are not assigning the result to anything, it does nothing- according to PEP8, you should put underscores between the words in variable and function names -
number_of_tries
vsnumberoftries
ordigitlist
vsdigit_list
,get_number()
vsgetnumber()
- following the previous note -
N
is not a good variable name - remove unused import
count = count + 1
can be rewritten ascount += 1
- put the main logic of the program into
if __name__ == '__main__':
- use
print()
function instead of a statement for Python 3.x compatibility - use
with
context manager when opening file(s) - this will ensure it being properly and safely closed - use
os.path.join()
to join the path components (okay, you now need to get yourimport os
back :) - define constants in upper case (PEP8 reference)
- use list comprehensions
- use
.format()
instead of string concatenation - use built-in
sum()
function to for counting - loop over the items directly instead of indexes
N.rstrip()
does not modify a string in place and returns a new string instead. Since you are not assigning the result to anything, it does nothing- according to PEP8, you should put underscores between the words in variable and function names -
number_of_tries
vsnumberoftries
ordigitlist
vsdigit_list
,get_number()
vsgetnumber()
- following the previous note -
N
is not a good variable name - remove unused import
count = count + 1
can be rewritten ascount += 1
- put the main logic of the program into
if __name__ == '__main__':
- use
print()
function instead of a statement for Python 3.x compatibility - use
with
context manager when opening file(s) - this will ensure it being properly and safely closed - use
os.path.join()
to join the path components (okay, you now need to get yourimport os
back :) - define constants in upper case (PEP8 reference)
- use list comprehensions
- use
.format()
instead of string concatenation - use built-in
sum()
function to for counting - loop over the items directly instead of indexes
N.rstrip()
does not modify a string in placedoes not modify a string in place and returns a new string instead. Since you are not assigning the result to anything, it does nothing- according to PEP8PEP8, you should put underscores between the words in variable and function names -
number_of_tries
vsnumberoftries
ordigitlist
vsdigit_list
,get_number()
vsgetnumber()
- following the previous note -
N
is not a good variable name - remove unused import
count = count + 1
can be rewritten ascount += 1
- put the main logic of the program into
if __name__ == '__main__':
- use
print()
function instead of a statement for Python 3.x compatibility - use
with
context manager when opening file(s) - this will ensure it being properly and safely closed - use
os.path.join()
to join the path components (okay, you now need to get yourimport os
back :) - define constants in upper case (PEP8 reference)
- use list comprehensions
- use
.format()
instead of string concatenation - use built-in
sum()
function to for counting - loop over the items directly instead of indexes
Alternative Solution:
We can also improve on both the performance and readability if we would use setssets
- collecting the digits seen so far until the length of the set is 10 (collected all digits) or we reach the maximum number of tries:
def get_number(number, number_of_tries):
print("new case numberdigits = {number}".formatset(number=number)[]) # keep digit_listtrack =of set([])all seen digits
current_number = number
for j in range(1, number_of_tries):
digit_listdigits |= set(str(current_number)) # add new digits to the seen set
if len(digit_listdigits) == 10: # exit if collected all the digits
return current_number
else:
current_number = number * (j + 1)
return "INSOMNIA"
N.rstrip()
does not modify a string in place and returns a new string instead. Since you are not assigning the result to anything, it does nothing- according to PEP8, you should put underscores between the words in variable and function names -
number_of_tries
vsnumberoftries
ordigitlist
vsdigit_list
,get_number()
vsgetnumber()
- following the previous note -
N
is not a good variable name - remove unused import
count = count + 1
can be rewritten ascount += 1
- put the main logic of the program into
if __name__ == '__main__':
- use
print()
function instead of a statement for Python 3.x compatibility - use
with
context manager when opening file(s) - this will ensure it being properly and safely closed - use
os.path.join()
to join the path components (okay, you now need to get yourimport os
back :) - define constants in upper case (PEP8 reference)
- use list comprehensions
- use
.format()
instead of string concatenation - use built-in
sum()
function to for counting - loop over the items directly instead of indexes
We can also improve on both the performance and readability if we would use sets:
def get_number(number, number_of_tries):
print("new case number = {number}".format(number=number)) digit_list = set([])
current_number = number
for j in range(1, number_of_tries):
digit_list |= set(str(current_number))
if len(digit_list) == 10:
return current_number
else:
current_number = number * (j + 1)
return "INSOMNIA"
N.rstrip()
does not modify a string in place and returns a new string instead. Since you are not assigning the result to anything, it does nothing- according to PEP8, you should put underscores between the words in variable and function names -
number_of_tries
vsnumberoftries
ordigitlist
vsdigit_list
,get_number()
vsgetnumber()
- following the previous note -
N
is not a good variable name - remove unused import
count = count + 1
can be rewritten ascount += 1
- put the main logic of the program into
if __name__ == '__main__':
- use
print()
function instead of a statement for Python 3.x compatibility - use
with
context manager when opening file(s) - this will ensure it being properly and safely closed - use
os.path.join()
to join the path components (okay, you now need to get yourimport os
back :) - define constants in upper case (PEP8 reference)
- use list comprehensions
- use
.format()
instead of string concatenation - use built-in
sum()
function to for counting - loop over the items directly instead of indexes
Alternative Solution:
We can also improve on both the performance and readability if we would use sets
- collecting the digits seen so far until the length of the set is 10 (collected all digits) or we reach the maximum number of tries:
def get_number(number, number_of_tries):
digits = set([]) # keep track of all seen digits
current_number = number
for j in range(1, number_of_tries):
digits |= set(str(current_number)) # add new digits to the seen set
if len(digits) == 10: # exit if collected all the digits
return current_number
else:
current_number = number * (j + 1)
return "INSOMNIA"
import os
NUMBER_OF_TRIES = 1000
BASE_DIRECTORY = '/home/username/GoogleCodeJam/'
INPUT_FILE_NAME = 'A-large-practice.in'
OUTPUT_FILE_NAME = 'largeoutput'
def get_number(number, number_of_tries):
print("new case number = {number}".format(number=number))
digit_list = []
current_number = number
for j in range(1, number_of_tries):
number_as_string = str(numbercurrent_number)
digits = []
for item in number_as_string:
if item not in digits:
digits.append(item)
for digit in digits:
if digit not in digit_list:
digit_list.append(digit)
count = sum(1 for k in range(0, 10) if str(k) in digit_list)
if count == 10:
print("count = 10")
return number_as_string
else:
current_number = number *=* (j + 1)
print("number = {number}".format(number=number))
return "INSOMNIA"
if __name__ == '__main__':
input_file_name = os.path.join(BASE_DIRECTORY, INPUT_FILE_NAME)
output_file_name = os.path.join(BASE_DIRECTORY, OUTPUT_FILE_NAME)
with open(input_file_name, "r") as input_file, open(output_file_name, "w") as output_file:
number_of_cases = next(input_file)
for case_number in range(1, int(number_of_cases) + 1):
number = int(next(input_file))
result = get_number(number, NUMBER_OF_TRIES)
output_file.write('Case #{case_number}: {result}\n'.format(case_number=case_number, result=result))
We can also improve on both the performance and readability if we would use sets:
def get_number(number, number_of_tries):
print("new case number = {number}".format(number=number))
digit_list = set([])
current_number = number
for j in range(1, number_of_tries):
digit_list |= set(str(current_number))
if len(digit_list) == 10:
return current_number
else:
current_number = number * (j + 1)
return "INSOMNIA"
import os
NUMBER_OF_TRIES = 1000
BASE_DIRECTORY = '/home/username/GoogleCodeJam/'
INPUT_FILE_NAME = 'A-large-practice.in'
OUTPUT_FILE_NAME = 'largeoutput'
def get_number(number, number_of_tries):
print("new case number = {number}".format(number=number))
digit_list = []
for j in range(1, number_of_tries):
number_as_string = str(number)
digits = []
for item in number_as_string:
if item not in digits:
digits.append(item)
for digit in digits:
if digit not in digit_list:
digit_list.append(digit)
count = sum(1 for k in range(0, 10) if str(k) in digit_list)
if count == 10:
print("count = 10")
return number_as_string
else:
number *= j + 1
print("number = {number}".format(number=number))
return "INSOMNIA"
if __name__ == '__main__':
input_file_name = os.path.join(BASE_DIRECTORY, INPUT_FILE_NAME)
output_file_name = os.path.join(BASE_DIRECTORY, OUTPUT_FILE_NAME)
with open(input_file_name, "r") as input_file, open(output_file_name, "w") as output_file:
number_of_cases = next(input_file)
for case_number in range(1, int(number_of_cases) + 1):
number = int(next(input_file))
result = get_number(number, NUMBER_OF_TRIES)
output_file.write('Case #{case_number}: {result}\n'.format(case_number=case_number, result=result))
import os
NUMBER_OF_TRIES = 1000
BASE_DIRECTORY = '/home/username/GoogleCodeJam/'
INPUT_FILE_NAME = 'A-large-practice.in'
OUTPUT_FILE_NAME = 'largeoutput'
def get_number(number, number_of_tries):
print("new case number = {number}".format(number=number))
digit_list = []
current_number = number
for j in range(1, number_of_tries):
number_as_string = str(current_number)
digits = []
for item in number_as_string:
if item not in digits:
digits.append(item)
for digit in digits:
if digit not in digit_list:
digit_list.append(digit)
count = sum(1 for k in range(0, 10) if str(k) in digit_list)
if count == 10:
print("count = 10")
return number_as_string
else:
current_number = number * (j + 1)
print("number = {number}".format(number=number))
return "INSOMNIA"
if __name__ == '__main__':
input_file_name = os.path.join(BASE_DIRECTORY, INPUT_FILE_NAME)
output_file_name = os.path.join(BASE_DIRECTORY, OUTPUT_FILE_NAME)
with open(input_file_name, "r") as input_file, open(output_file_name, "w") as output_file:
number_of_cases = next(input_file)
for case_number in range(1, int(number_of_cases) + 1):
number = int(next(input_file))
result = get_number(number, NUMBER_OF_TRIES)
output_file.write('Case #{case_number}: {result}\n'.format(case_number=case_number, result=result))
We can also improve on both the performance and readability if we would use sets:
def get_number(number, number_of_tries):
print("new case number = {number}".format(number=number))
digit_list = set([])
current_number = number
for j in range(1, number_of_tries):
digit_list |= set(str(current_number))
if len(digit_list) == 10:
return current_number
else:
current_number = number * (j + 1)
return "INSOMNIA"