The Student Room Group

Comp Sci -python

This thread is for every python to post their questions and answers. It is created to improve your skills by tackling each other questions
Other python beginners can post their answers



Q: Write a function called pattern_sum that receives two single digit positive integers, (k and m) as parameters and calculates and returns the total sum as:
k + kk + kkk + .... (the last number in the sequence should have m digits)
For example, if the two integers are:

(4, 5)
Your function should return the total sum of:
4 + 44 + 444 + 4444 + 44444
Notice the last number in this sequence has 5 digits. The return value should be:
49380

A:

Spoiler

(edited 7 years ago)

Scroll to see replies

Reply 1
Q: Write a function named unique_common that accepts two lists both of which contain integers as parameters and returns a sorted list (ascending order) which contains unique common elements from both the lists. If there are no common elements between the two lists, then your function should return the keyword None

For example, if two of the lists received by the function are:

([5, 6, -7, 8, 8, 9, 9, 10], [2, 4, 8, 8, 5, -7])
You can see that elements 5, -7, and 8 are common in both the first list and the second list and that the element 8 occurs twice in both lists. Now you should return a sorted list (ascending order) of unique common elements like this:
[-7, 5, 8]
if the two lists received by the function are:
([5, 6, 7, 0], [3, 2, 3, 2])
Since, there are no common elements between the two lists, your function should return the keyword
None


A:

Spoiler

(edited 7 years ago)
Reply 2
Q: Write a function named find_gcd that accepts a list of positive integers and returns their greatest common divisor (GCD). Your function should return 1 as the GCD if there are no common factors between the integers. Here are some examples:

if the list was

[12, 24, 6, 18]
then your function should return the GCD:
6



A:

Spoiler

(edited 7 years ago)
Reply 3
Q: Write a function that accepts an input string consisting of alphabetic characters and removes all the trailing whitespace of the string and returns it without using any .strip() method. For example if:

input_string = " Hello "
then your function should return an output string such as:
output_string = " Hello"

A:

Spoiler

Reply 4
Q: Write a function which accepts an input string consisting of alphabetic characters and spaces and returns the string with all the spaces removed. Do NOT use any string methods for this problem.

A:

Spoiler

Reply 5
Q:Write a function which accepts an input string and returns a string where the case of the characters are changed, i.e. all the uppercase characters are changed to lower case and all the lower case characters are changed to upper case. The non-alphabetic characters should not be changed. Do NOT use the string methods upper(), lower(), or swap().
A:

Spoiler

Reply 6
Q:
Write a function that takes a string consisting of alphabetic characters as input argument and returns True if the string is a palindrome. A palindrome is a string which is the same backward or forward. Note that capitalization does not matter here i.e. a lower case character can be considered the same as an upper case character.


A:

Spoiler

Reply 7
Q: Write a function that accepts a string and a character as input and returns the count of all the words in the string which start with the given character. Assume that capitalization does not matter here. You can assume that the input string is a sentence i.e. words are separated by spaces and consists of alphabetic characters.

A:

Spoiler

One liners. :smile:

Original post by bigmansouf
Q: Write a function called pattern_sum that receives two single digit positive integers, (k and m) as parameters and calculates and returns the total sum as:
k + kk + kkk + .... (the last number in the sequence should have m digits)
def pattern_sum(k, m):
return sum([int(str(k) * a) for a in range(1, m + 1)])
Original post by bigmansouf
Q: Write a function named unique_common that accepts two lists both of which contain integers as parameters and returns a sorted list (ascending order) which contains unique common elements from both the lists. If there are no common elements between the two lists, then your function should return the keyword None
def unique_common(a, b):
return sorted(list(set(a).intersection(set(b)))) or None
Original post by bigmansouf
Q:Write a function that takes a string consisting of alphabetic characters as input argument and returns True if the string is a palindrome. A palindrome is a string which is the same backward or forward. Note that capitalization does not matter here i.e. a lower case character can be considered the same as an upper case character.
def case_insensitive_palindrome(s):
return s.lower() == s.lower()[::-1]
Original post by bigmansouf
Q: Write a function which accepts an input string consisting of alphabetic characters and spaces and returns the string with all the spaces removed. Do NOT use any string methods for this problem.
def strip_whitespace(s):
return reduce(lambda x, y: x + y, [x])
Original post by bigmansouf
Q: Write a function that accepts a string and a character as input and returns the count of all the words in the string which start with the given character. Assume that capitalization does not matter here. You can assume that the input string is a sentence i.e. words are separated by spaces and consists of alphabetic characters.
def count_first_letter(s, c):
return len([word for word in s.split(' ') if word[0] == c])
.
(edited 7 years ago)
Reply 9
Original post by Push_More_Button
One liners. :smile:
.


Amazing

how long did it take you to mastr python
how do you get to one liners style of coding
because i have seen it on stack overflow but not in most beginners python book

which books can help me improve my skills


thank you
Q: Write a function named count_consonants that receives a string as parameter and returns the total count of the consonants in the string. Consonants are all the characters in the english alphabet except for 'a', 'e', 'i', 'o', 'u'. If the same consonant repeats multiple times you should count all of them. Note that capitalization and punctuation does not matter here i.e. a lower case character should be considered the same as an upper case character.


A: def cons(str1):
str1 = str1.replace(" ", "").lower()
print str1
a = ["a", "e", "i", "o", "u"]
count = 0
for i in str1:
if i in a:
continue
count += 1
return count
Original post by bigmansouf
Amazing

how long did it take you to mastr python
how do you get to one liners style of coding
because i have seen it on stack overflow but not in most beginners python book

which books can help me improve my skills


thank you


I've been using Python for over two years now and it's by far my favourite language so I've practised with it lots. I'm also currently working as a Python Developer so I use it rather often. :smile:

There's a lot of great functionality in the standard library and it's possible to do quite a lot with it which is obviously helpful for writing nice short bits of Pythonic code, but in pretty much all cases readability trumps one liners or magic in the real world as it's easier to maintain and modify so don't aim to write one liners, try to write nice readable Pythonic code.

Best tip I can give you is practise and to read up on what's possible to do with the standard library and data structures. (Lists and dictionaries are super powerful btw if you look at them as more than a simple data storage)
Original post by bigmansouf
Q: Write a function named find_gcd that accepts a list of positive integers and returns their greatest common divisor (GCD). Your function should return 1 as the GCD if there are no common factors between the integers. Here are some examples:

if the list was

[12, 24, 6, 18]
then your function should return the GCD:
6

A:

Spoiler



You can also use Euclid's algorithm and the fact that gcd(a,b,c) = gcd(a, gcd(b,c)). This way, the runtime won't be harmed by suspicious inputs, such as two very large primes.
Original post by Jooooshy
You can also use Euclid's algorithm and the fact that gcd(a,b,c) = gcd(a, gcd(b,c)). This way, the runtime won't be harmed by suspicious inputs, such as two very large primes.


thanks
Original post by Push_More_Button
I've been using Python for over two years now and it's by far my favourite language so I've practised with it lots. I'm also currently working as a Python Developer so I use it rather often. :smile:

There's a lot of great functionality in the standard library and it's possible to do quite a lot with it which is obviously helpful for writing nice short bits of Pythonic code, but in pretty much all cases readability trumps one liners or magic in the real world as it's easier to maintain and modify so don't aim to write one liners, try to write nice readable Pythonic code.

Best tip I can give you is practise and to read up on what's possible to do with the standard library and data structures. (Lists and dictionaries are super powerful btw if you look at them as more than a simple data storage)


thank you
Original post by bigmansouf
Q: Write a function named count_consonants that receives a string as parameter and returns the total count of the consonants in the string. Consonants are all the characters in the english alphabet except for 'a', 'e', 'i', 'o', 'u'. If the same consonant repeats multiple times you should count all of them. Note that capitalization and punctuation does not matter here i.e. a lower case character should be considered the same as an upper case character.

Spoiler

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.



A:

Spoiler

(edited 7 years ago)
Original post by Push_More_Button
I've been using Python for over two years now and it's by far my favourite language so I've practised with it lots. I'm also currently working as a Python Developer so I use it rather often. :smile:

There's a lot of great functionality in the standard library and it's possible to do quite a lot with it which is obviously helpful for writing nice short bits of Pythonic code, but in pretty much all cases readability trumps one liners or magic in the real world as it's easier to maintain and modify so don't aim to write one liners, try to write nice readable Pythonic code.

Best tip I can give you is practise and to read up on what's possible to do with the standard library and data structures. (Lists and dictionaries are super powerful btw if you look at them as more than a simple data storage)

Spoiler

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

Spoiler

(edited 7 years ago)
Original post by morgan8002

Spoiler



i put a different answer my mistake

Quick Reply

Latest