Python Forum
Beginner question about code comparison
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner question about code comparison
#1
Hi folks, I've just started learning programming and as I was making my way through various online beginner exercises, I came across one that asked to write a basic program which required to copy (n) times the first two characters of any given string and if the string is shorter than two characters- copy the first character (n) times.

My idea of going about it was this:
def substring_copy(string, copies):
    if len(string) >= 2:
        return string[:2] * copies
    return string * copies
print(substring_copy('abcdef', 2))
print(substring_copy('p', 3))
The actual answer for the exercise was:
def substring_copy(str, n):
    flen = 2
    if flen > len(str):
        flen = len(str)
    substr = str[:flen]

    result = ""
    for i in range(n):
        result = result + substr
    return result
print(substring_copy('abcdef', 2))
print(substring_copy('p', 3));
They both give the same result and for sure I always feel delighted to write a shorter block of code, however before I get into a habit of writing short but unstable blocks of code any advice on what to avoid? Or why one way is better than the other?


here's another exercise example to check if a number exists in given list:
My way:
def look_for_nr(nr_list, nr):
    return nr in nr_list
print(look_for_nr([1, 5, 8, 3], 3))
print(look_for_nr([5, 8, 3], -1))
Actual answer:
def look_for_nr(nr_list, nr):
    for value in nr_list:
        if nr == value:
            return True
    return False
print(look_for_nr([1, 5, 8, 3], 3))
print(look_for_nr([5, 8, 3], -1))
Any feedback highly appreciated !
Reply
#2
About the second exercise - the first snippet is the better/preferred way to write it.

Now, about the first exercise. Again the first snippet is better, but still can be simplified and improved. What makes you think it is "unstable"? This is simple function, but in any case proper testing will help to make sure it's upto your standards and stable enough for your taste.
Now, how it could be simplified:
>>> spam = 'a'
>>> spam[:2]
'a'
As you can see using slicing does not rise error if slice extends beyond the iterable len, so
def substring_copy(my_string, copies):
    return my_string[:2] * copies
Also, don't use string and str as names. string is a module from standard library and str is a built-in function/type.
GJG likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
It's unbelievable how you're being thought to solve a problem in 10 lines while it could be done in 2 all along, but I guess it's all for better understanding of how it all works and practising computational thinking. Don't know how to feel about these exercises anymore LOL . Thanks for taking time and tips buran! Maybe you could recommend some articles or any sources on how to test your code?
Reply
#4
Ned Batchelder's Getting Started Testing is a nice introduction to testing. There is also link to youtube video, if you prefer.
I like pytest as a test framework
GJG likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020