The Student Room Group

Comp Sci -python

Scroll to see replies

Original post by bigmansouf
i put a different answer my mistake


Are you answering the same question? Yours seems to be finding the longest word in a string.
Original post by morgan8002
Are you answering the same question? Yours seems to be finding the longest word in a string.


I did i mistake i puloaded the wrong question
however in the case of the question that you did i panicked and didnt read the question probably. I thought it was asking me to find an anagram of any word. you did a great job
thank you
Original post by bigmansouf
I did i mistake i puloaded the wrong question
however in the case of the question that you did i panicked and didnt read the question probably. I thought it was asking me to find an anagram of any word. you did a great job
thank you

Ah ok.

This does the same as your answer:def find(s):
d = {len(word): word for word in s.lower().split()}
return d[max(d.keys())]
Q:
Write a function that accepts a string which contains a particular date from the Gregorian calendar. Your function should return what day of the week it was. Here are a few examples of what the input string(Month Day Year) will look like. However, you should not 'hardcode' your program to work only for these input!

"June 12 2012"
"September 3 1955"
"August 4 1843"
Note that each item (Month Day Year) is separated by one space. For example if the input string is:
"May 5 1992"
Then your function should return the day of the week (string) such as:
"Tuesday"
Algorithm with sample example:
# Assume that input was "May 5 1992"
day (d) = 5 # It is the 5th day
month (m) = 3 # (*** Count starts at March i.e March = 1, April = 2, ... January = 11, February = 12)
century (c) = 19 # the first two characters of the century
year (y) = 92 # Year is 1992 (*** if month is January or february decrease one year)
# Formula and calculation
day of the week (w) = (d + floor(2.6m - 0.2) - 2c + y + floor(y/4) + floor(c/4)) modulo 7
after calculation we get, (w) = 2
Count for the day of the week starts at Sunday, i.e Sunday = 0, Monday = 1, Tuesday = 2, ... Saturday = 6
Since we got 2, May 5 1992 was a Tuesday


A:

Spoiler

Original post by bigmansouf
Q: Write a function named find_longest_word that receives a string as parameter and returns the longest word in the string. Assume that the input to this function is a string of words consisting of alphabetic characters that are separated by space(s). In case of a tie between some words return the last one that occurs.


find_longest_word = lambda s: sorted(s.split(' '), key=len)[-1]

Original post by bigmansouf
Q Write a function named test_for_anagrams that receives two strings as parameters, both of which consist of alphabetic characters and returns True if the two strings are anagrams, False otherwise. Two strings are anagrams if one string can be constructed by rearranging the characters in the other string using all the characters in the original string exactly once. For example, the strings "Orchestra" and "Carthorse" are anagrams because each one can be constructed by rearranging the characters in the other one using all the characters in one of them exactly once. Note that capitalization does not matter here i.e. a lower case character can be considered the same as an upper case character

def test_for_anagrams(first_word, second_word):
return sorted(first_word) == sorted(second_word)


Original post by bigmansouf
Q
Write a function that accepts a string which contains a particular date from the Gregorian calendar. Your function should return what day of the week it was. Here are a few examples of what the input string(Month Day Year) will look like. However, you should not 'hardcode' your program to work only for these input!

def get_weekday(date_string):
return datetime.datetime.strptime(date_string, "%B %d %Y").strftime("%A")
Original post by fat_hampster


def get_weekday(date_string):
return datetime.datetime.strptime(date_string, "%B %d %Y").strftime("%A")

awesome
never thought this is possible in short style of coding
thanks
Q: Write a function that takes a 4 digit integer (from 1000 to 9999 both inclusive) as input argument and returns the integer using words as shown below:

Sample Examples:
if the input integer is 7000 then the function should return the string "seven thousand"
if the input integer is 1008 then the function should return the string "one thousand eight"
if the input integer is 4010 then the function should return the string "four thousand ten"
if the input integer is 1012 then the function should return the string "one thousand twelve"
if the input integer is 4506 then the function should return the string "four thousand five hundred six"
if the input integer is 9900 then the function should return the string "nine thousand nine hundred"
if the input integer is 8880 then the function should return the string "eight thousand eight hundred eighty"
if the input integer is 5432 then the function should return the string "five thousand four hundred thirty two"
Notice that the words in the output strings should all be lower cased and separated by only one space and make sure your words match the following spellings:
one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen,
sixteen, seventeen, eighteen, nineteen, twenty, thirty, forty, fifty, sixty, seventy, eighty,
ninety, hundred, thousand


A:

Spoiler

Reply 27
Loving this thread. Will be very useful over summer as I learn python and C++ :smile:
Original post by TheBBQ
Loving this thread. Will be very useful over summer as I learn python and C++ :smile:


thanks
Q:
Write a function named construct_dictionary_from_lists that accepts 3 one dimensional lists and constructs and returns a dictionary as specified below.
The first one dimensional list will have n strings which will be the names of some people.
The second one dimensional list will have n integers which will be the ages that correspond to those people.
The third one dimensional list will have n integers which will be the scores that correspond to those people.
Note that if a person has a score of 60 or higher (score >= 60) that means the person passed, if not the person failed.
Your function should return a dictionary where each key is the name of a person and the value corresponding to that key should be a list containing age, score, 'pass' or 'fail' in the same order as shown in the sample output.
For example, if the function receives the following lists:

(["paul", "saul", "steve", "chimpy"], [28, 59, 22, 5], [59, 85, 55, 60])
then your function should return the dictionary such as:
{'steve': [22, 55, 'fail'], 'saul': [59, 85, 'pass'], 'paul': [28, 59, 'fail'], 'chimpy': [5, 60, 'pass']}

A:

Spoiler

Q: Write a function named one_to_2D which receives an input list and two integers r and c as parameters and returns a new two-dimensional list having r rows and c columns.

Note that if the number of elements in the input list is larger than r*c then ignore the extra elements. If the number of elements in the input list is less than r*c then fill the two dimensional list with the keyword None. For example if your fuction is called as hown below:

one_to_2D([8, 2, 9, 4, 1, 6, 7, 8, 7, 10], 2, 3)
Then it should return:
[[8, 2, 9],[4, 1, 6]]

A:

Spoiler

Q: Write a function named multiplication_table that receives a positive integer 'n' as parameter and returns a n by n multiplication table as a two-dimensional list.

For example if the integer received by the function 'n' is:

4
then your program should return a two_dimensional list with 4 rows and 4 columns such as:

[[1, 2, 3, 4],
[2, 4, 6, 8],
[3, 6, 9, 12],
[4, 8, 12, 16]]


A:

Spoiler

Original post by bigmansouf
Q:
Write a function named construct_dictionary_from_lists that accepts 3 one dimensional lists and constructs and returns a dictionary as specified below.


Can't think of a way to sort it how you specified in a one liner, but then I ran your code and this is the output:
{'steve': [22, 55, 'fail'], 'paul': [28, 59, 'fail'], 'chimpy': [5, 60, 'pass'], 'saul': [59, 85, 'pass']}
def construct_dictionary_from_lists(names, ages, scores):
return {name: [ages, scores, 'pass' if scores >= 60 else 'fail'] for i, name in enumerate(names)}
Returns:
{'steve': [22, 55, 'fail'], 'paul': [28, 59, 'fail'], 'chimpy': [5, 60, 'pass'], 'saul': [59, 85, 'pass']}
.
Original post by bigmansouf
Q: Write a function that takes a 4 digit integer (from 1000 to 9999 both inclusive) as input argument and returns the integer using words as shown below:

def integer_to_string(n):
n, units, tens, non_standard = [int(x) for x in str(n)], ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"], ["", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"], ["eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
return units[n[0]] + " thousand " + units[n[1]] + " hundred " * (n[1] != 0) + (non_standard[n[3] - 1] if 10 * n[2] + n[3] in [11, 12, 13, 14, 15, 16, 17, 18, 19] else tens[n[2]] + " " * (n[2] != 0) + units[n[3]])
Original post by bigmansouf
Q:
Write a function named construct_dictionary_from_lists that accepts 3 one dimensional lists and constructs and returns a dictionary as specified below.

def construct_dictionary(a, b, c):
return {a[n]: [b[n], c[n], ["fail", "pass"][int(c[n] >= 60)]] for n in range(len(a))}
Original post by bigmansouf
Q: Write a function named one_to_2D which receives an input list and two integers r and c as parameters and returns a new two-dimensional list having r rows and c columns.

def one_to_2D(a, r, c):
return [a[n * c: (n + 1) * c] for n in range(r)]
(edited 8 years ago)
Original post by bigmansouf
Q: Write a function named multiplication_table that receives a positive integer 'n' as parameter and returns a n by n multiplication table as a two-dimensional list.


def multiplication_table(n):
return [[i * j for i in range(1, n + 1)]for j in range(1, n + 1)]
Original post by Push_More_Button
Can't think of a way to sort it how you specified in a one liner, but then I ran your code and this is the output:
{'steve': [22, 55, 'fail'], 'paul': [28, 59, 'fail'], 'chimpy': [5, 60, 'pass'], 'saul': [59, 85, 'pass']}
def construct_dictionary_from_lists(names, ages, scores):
return {name: [ages, scores, 'pass' if scores >= 60 else 'fail'] for i, name in enumerate(names)}
Returns:
{'steve': [22, 55, 'fail'], 'paul': [28, 59, 'fail'], 'chimpy': [5, 60, 'pass'], 'saul': [59, 85, 'pass']}
.


thanks i cheated a bit by looking at stackover flow


im not good at one liner
Original post by morgan8002

def multiplication_table(n):
return [[i * j for i in range(1, n + 1)]for j in range(1, n + 1)]


great
im going to find a question u cant one-line
Q: Write a function that accepts a filename as input argument and reads the file and saves each line of the file as an element in a list (without the new line ("\n")character) and returns the list. Each line of the file has comma separated values: For example if the content of the file looks like this:

Lucas,Turing,22
Alan,Williams,27
Layla,Trinh,21
Brayden,Huang,22
Oliver,Greek,20
then your function should return a list such as
['Lucas,Turing,22', 'Alan,Williams,27', 'Layla,Trinh,21', 'Brayden,Huang,22', 'Oliver,Greek,20']

A:

Spoiler




i got the right answer but the auto-corrector says no i think it is right
Original post by bigmansouf
Q: Write a function that accepts a filename as input argument and reads the file and saves each line of the file as an element in a list (without the new line ("\n":wink:character) and returns the list. Each line of the file has comma separated values:



def open_file(file_name):
return open(file_name).readlines()
Original post by morgan8002

def open_file(file_name):
return open(file_name).readlines()



you forgot to remove the \n newline at the end
def list_from_file(file_name):
# Make a connection to the file
file_pointer = open(file_name, 'r')
# You can use either .read() or .readline() or .readlines()
data = file_pointer.readlines()
# NOW CONTINUE YOUR CODE FROM HERE!!!
list_of_lines = []
for line in data:
list_of_lines.append(line.strip())
return list_of_lines

Quick Reply

Latest

Trending

Trending