Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

I'm doing a CodeEval challenge, where the object is to find a famous writers name and a year, inside of a random given string.

You have a set of rows with names of famous writers encoded inside. Each row is divided into 2 parts by pipe char (|). The first part has a writer's name. The second part is a "key" to generate a name.

Your goal is to go through each number in the key (numbers are separated by space) left-to-right. Each number represents a position in the 1st part of a row. This way you collect a writer's name which you have to output.

Challenge input:

osSE5Gu0Vi8WRq93UvkYZCjaOKeNJfTyH6tzDQbxFm4M1ndXIPh27wBA rLclpg| 3 35 27 62 51 27 46 57 26 10 46 63 57 45 15 43 53

3Kucdq9bfCEgZGF2nwx8UpzQJyHiOm0hoaYP6ST1WM7Nks5XjrR4IltBeDLV vA| 2 26 33 55 34 50 33 61 44 28 46 32 28 30 3 50 34 61 40 7 1 31

Expected output:

Stephen King 1947

Kyotaro Nishimura 1930


I've successfully solved this problem and would like some critique on my work:

import sys
def find_a_writer(string, int_list):
 """ Find a famous writer and a year in a random string
 >>> print(find_a_writer("osSE5Gu0Vi8WRq93UvkYZCjaOKeNJfTyH6tzDQbxFm4M1ndXIPh27wBA rLclpg",
 >>> ['3', '35', '27', '62', '51', '27', '46', '57', '26', '10', '46', '63', '57', '45', '15', '43', '53']))
 Stephen King 1947"""
 results = []
 string_list = list(string)
 for i in int_list:
 results.append(string_list[int(i) - 1])
 return ''.join(results)
if __name__ == '__main__':
 with open(sys.argv[1]) as data:
 for line in data.readlines():
 data_arr = line.rstrip().strip().split("|")
 print(find_a_writer(data_arr[0].strip(), data_arr[1].strip().split(" ")))

What I would like to know:

  1. What did I do well?
  2. Is there anything I can do better?
  3. Can I shorten the function?

I'm doing a CodeEval challenge, where the object is to find a famous writers name and a year, inside of a random given string.

You have a set of rows with names of famous writers encoded inside. Each row is divided into 2 parts by pipe char (|). The first part has a writer's name. The second part is a "key" to generate a name.

Your goal is to go through each number in the key (numbers are separated by space) left-to-right. Each number represents a position in the 1st part of a row. This way you collect a writer's name which you have to output.

Challenge input:

osSE5Gu0Vi8WRq93UvkYZCjaOKeNJfTyH6tzDQbxFm4M1ndXIPh27wBA rLclpg| 3 35 27 62 51 27 46 57 26 10 46 63 57 45 15 43 53

3Kucdq9bfCEgZGF2nwx8UpzQJyHiOm0hoaYP6ST1WM7Nks5XjrR4IltBeDLV vA| 2 26 33 55 34 50 33 61 44 28 46 32 28 30 3 50 34 61 40 7 1 31

Expected output:

Stephen King 1947

Kyotaro Nishimura 1930


I've successfully solved this problem and would like some critique on my work:

import sys
def find_a_writer(string, int_list):
 """ Find a famous writer and a year in a random string
 >>> print(find_a_writer("osSE5Gu0Vi8WRq93UvkYZCjaOKeNJfTyH6tzDQbxFm4M1ndXIPh27wBA rLclpg",
 >>> ['3', '35', '27', '62', '51', '27', '46', '57', '26', '10', '46', '63', '57', '45', '15', '43', '53']))
 Stephen King 1947"""
 results = []
 string_list = list(string)
 for i in int_list:
 results.append(string_list[int(i) - 1])
 return ''.join(results)
if __name__ == '__main__':
 with open(sys.argv[1]) as data:
 for line in data.readlines():
 data_arr = line.rstrip().strip().split("|")
 print(find_a_writer(data_arr[0].strip(), data_arr[1].strip().split(" ")))

What I would like to know:

  1. What did I do well?
  2. Is there anything I can do better?
  3. Can I shorten the function?

I'm doing a CodeEval challenge, where the object is to find a famous writers name and a year, inside of a random given string.

You have a set of rows with names of famous writers encoded inside. Each row is divided into 2 parts by pipe char (|). The first part has a writer's name. The second part is a "key" to generate a name.

Your goal is to go through each number in the key (numbers are separated by space) left-to-right. Each number represents a position in the 1st part of a row. This way you collect a writer's name which you have to output.

Challenge input:

osSE5Gu0Vi8WRq93UvkYZCjaOKeNJfTyH6tzDQbxFm4M1ndXIPh27wBA rLclpg| 3 35 27 62 51 27 46 57 26 10 46 63 57 45 15 43 53

3Kucdq9bfCEgZGF2nwx8UpzQJyHiOm0hoaYP6ST1WM7Nks5XjrR4IltBeDLV vA| 2 26 33 55 34 50 33 61 44 28 46 32 28 30 3 50 34 61 40 7 1 31

Expected output:

Stephen King 1947

Kyotaro Nishimura 1930


I've successfully solved this problem and would like some critique on my work:

import sys
def find_a_writer(string, int_list):
 """ Find a famous writer and a year in a random string
 >>> print(find_a_writer("osSE5Gu0Vi8WRq93UvkYZCjaOKeNJfTyH6tzDQbxFm4M1ndXIPh27wBA rLclpg",
 >>> ['3', '35', '27', '62', '51', '27', '46', '57', '26', '10', '46', '63', '57', '45', '15', '43', '53']))
 Stephen King 1947"""
 results = []
 string_list = list(string)
 for i in int_list:
 results.append(string_list[int(i) - 1])
 return ''.join(results)
if __name__ == '__main__':
 with open(sys.argv[1]) as data:
 for line in data.readlines():
 data_arr = line.rstrip().strip().split("|")
 print(find_a_writer(data_arr[0].strip(), data_arr[1].strip().split(" ")))

What I would like to know:

  1. What did I do well?
  2. Is there anything I can do better?
  3. Can I shorten the function?
Tweeted twitter.com/StackCodeReview/status/801509587698458624
Source Link
papasmurf
  • 421
  • 1
  • 4
  • 12

Find a famous writer in a random string

I'm doing a CodeEval challenge, where the object is to find a famous writers name and a year, inside of a random given string.

You have a set of rows with names of famous writers encoded inside. Each row is divided into 2 parts by pipe char (|). The first part has a writer's name. The second part is a "key" to generate a name.

Your goal is to go through each number in the key (numbers are separated by space) left-to-right. Each number represents a position in the 1st part of a row. This way you collect a writer's name which you have to output.

Challenge input:

osSE5Gu0Vi8WRq93UvkYZCjaOKeNJfTyH6tzDQbxFm4M1ndXIPh27wBA rLclpg| 3 35 27 62 51 27 46 57 26 10 46 63 57 45 15 43 53

3Kucdq9bfCEgZGF2nwx8UpzQJyHiOm0hoaYP6ST1WM7Nks5XjrR4IltBeDLV vA| 2 26 33 55 34 50 33 61 44 28 46 32 28 30 3 50 34 61 40 7 1 31

Expected output:

Stephen King 1947

Kyotaro Nishimura 1930


I've successfully solved this problem and would like some critique on my work:

import sys
def find_a_writer(string, int_list):
 """ Find a famous writer and a year in a random string
 >>> print(find_a_writer("osSE5Gu0Vi8WRq93UvkYZCjaOKeNJfTyH6tzDQbxFm4M1ndXIPh27wBA rLclpg",
 >>> ['3', '35', '27', '62', '51', '27', '46', '57', '26', '10', '46', '63', '57', '45', '15', '43', '53']))
 Stephen King 1947"""
 results = []
 string_list = list(string)
 for i in int_list:
 results.append(string_list[int(i) - 1])
 return ''.join(results)
if __name__ == '__main__':
 with open(sys.argv[1]) as data:
 for line in data.readlines():
 data_arr = line.rstrip().strip().split("|")
 print(find_a_writer(data_arr[0].strip(), data_arr[1].strip().split(" ")))

What I would like to know:

  1. What did I do well?
  2. Is there anything I can do better?
  3. Can I shorten the function?
lang-py

AltStyle によって変換されたページ (->オリジナル) /