Help for some basics exercise - Reconversion

Hi started to learn Python yesterday( never coding before, this is for a professional reconversion).
I am currently learning on this website: “Codecademy” and i have done the First Python Syntax chapter allready.
Yesterday they send exercises to test my level that i need to send this Friday! an learn the 7 other chapters at least to help me: (Strings and Console Output / Conditionals and Control Flow / Functions /Lists & Dictionaries /Student Becomes the Teacher /Lists and Functions /Loops)on Code Academy.

I am not asking to help me do this exercises,( until now). But is this possible to at least hint me what chapter can help me solve theses basic coding exercises? here there is the 2 screen links

you will need pretty much the entire python course, practice makes perfect is good for problem solving skills. And you need to learn to work with classes and files if i look at the assignments.

i would be very impressed if you can manage this before friday.

1 Like

I’d say you have some work ahead of you. There are no shortcuts.

For instance, Exercise 6 requires understanding of lists, loops, conditionals, logical operators and modulo division (remainder). The unit on advanced topics introduces how we can compress everything into a list comprehension, but if you don’t understand how to write one, don’t use it.

``````>>> [x for x in range(77, 778) if not x % 7 and x % 5]
[77, 84, 91, 98, 112, 119, 126, 133, 147, 154, 161, 168, 182, 189,
196, 203, 217, 224, 231, 238, 252, 259, 266, 273, 287, 294,
301, 308, 322, 329, 336, 343, 357, 364, 371, 378, 392, 399,
406, 413, 427, 434, 441, 448, 462, 469, 476, 483, 497, 504,
511, 518, 532, 539, 546, 553, 567, 574, 581, 588, 602, 609,
616, 623, 637, 644, 651, 658, 672, 679, 686, 693, 707, 714,
721, 728, 742, 749, 756, 763, 777]
>>>
``````

Thanks, i think this is more for evaluate my starting level. I see this is tough
Do you think i can at least focus on the firsts exercices ? (Exercises 2-4)? I am learning strings and console output right now

Exercise 2 can be done a couple of ways. One with the use of an algorithm, meaning a loop and a range, and one with the use of a proven mathematical formula. Do you know th formula to sum a sequence?

I dont know yet the formulate or the loop and a range, i think i will find them specifically thanks, if friday before midnight i didnt find the solution i am not against help and then continue my training at a better pace^^

Hello,
If i am understanding well,
for exercises 2 and 3 i check for loops.
For exercise 4 , i dont know …
For exercise 5 maybe around strings?
For exercise 6 loop and some arithmetic operations.
For exercise 7 maybe i have to learn classes?
For exercise 8 i dont know

1 Like

For now, just pour yourself into the Python course and make your way through the modules as best you can. Ask questions on the exercises you get stuck on and others can help you gain more understanding.

Remember, programming is not about knowing, but about thinking through a problem, which means understanding the problem first, then designing a program to solve it.

Things you need to know are `str` methods and functions such as `len()`, `.upper()`, `.lower()`, `.isupper()`, `.islower()`, etc. Also string slicing. Learn about `range()` and how to use it in various situations.

Loops and conditionals and control flow are two very important concepts to get rooted in. You will want to be able to visualize them when conceiving a solution or code pattern aimed at a particular result. Exercise 2 and 3 can be accomplished very simply using them (and `range()`). Study the examples in the lessons once you get to that unit.

@stetim94 refers to the `Practice Makes Perfect` unit where you get to explore your skills. Try to do all the exercises without looking forward, only backward through completed units. Don’t use solutions from the forums that you do not understand. All the questions can be solved with the basics learned in the previous units. Exercise # 9 is one you will solve in this unit, so just remember how you did it here, and apply it there. Repeat… Don’t use solutions you find posted. Learn from them and apply what you learn to your own solution so it is genuine and something you can do again, on the spot, if asked to.

You will need to understand classes to answer # 7, but what you learn here should be enough so long as you understand the difference between a class method and an instance method. That comes up so don’t race ahead.

While it may be sort of like cheating to Google the topics in your exercises, it is not prohibited (rather encouraged) while you are immersed in the track lessons. Read up a little as you learn new concepts. Refer to documentation on python.org (start with Python 2.7) so you get a deeper understanding of the actual language design, and a better familiarity with how the docs are organized and written. Keep a window open on repl.it (Python 2.7) while you learn so you have an interactive console to test statements in (when entered directly they are called commands). My earlier example was written directly into the command line.

``````>>> print ("this code will execute immediately")
``````

There is a unit on basic I/O and files. Take notes and you should be able to solve # 8 with what you learn there.

Exercise #4 requires the `os` module (and possibly `sys`). That means learning how to import modules. Hint: PING a website such as google.com.

Exercise #5 requires a good understanding of strings and how to parse and manipulate them. Again, loops come into play. There are a range of solutions, from brute force algorithms to regular expressions. Unfortunately, regex is not covered here in any great depth, if at all (in the main track) so you will need to use loops and conditionals. Once you tackle the Advanced Topics unit you will be well enough prepared to tackle this one using more advanced techniques (but you can still try it with a basic algo, for practice and experience).

The last exercise will require more information than can be got from this course. It is an age-old problem that can probably be found solved in any number of ways. Learn from what you find and embed the reasoning and concepts in your mind so you can devise your own methodology. Cheaters are easily called out when it comes to programming. What you struggle with yourself will imprint upon your thought processes so don’t cheat yourself out of that. Get help if you are really struggling though. It’s not uncommon to have a certain amount of bias and correction-blindness when we first get going. We’re not looking for better ways, just ways that work how we expect them to.

wow, i was not expecting a so detailed answer.You are awesome thank you ! I will try my best

1 Like
``````num = 2

while num <= 10:
print num
num += 2
if num == 12:
print "Goodbye"

``````

I have made a piece of code that work on exercise 3! i am aming now on fonctions for the second exercise^^

1 Like

That does not need a conditional. Just print Goodbye on its own unindented line.

Oh you are right!! thanks

1 Like

i would be lazy, i would use pythons range function, given you can specify start, stop and step value.

Just the same, it’s nice to see a learner actually use a `while` loop correctly. I agree that `range(2, 11, 2)` would be a simple solution.

``````Exercice 2)

result = sum(range(778))
print result

Exercice 6)

def Exercise6():
for x in range(77, 777):
if x % 5 == 0:
if x % 7 == 0:
print(x)

Exercice 9)

def is_prime(x):
if x < 2:
return False
else:
for n in range(2, x-1):
if x % n == 0:
return False
return True

``````

Ok guys, this is my progression atm, do you think my code is acceptable? i tried to keep this the simply possible.

That works, and is probably pretty fast. The real test is how long it takes to sum a million numbers, or a billion. No doubt it will be fast, but I can only speculate since I’m not a qualified bench tester.

The mathematical formula is,

``````s = n * (a + k) / 2
``````

where,

`s` is the sum of the sequence;
`n` is the number of terms in the sequence;
`a` is the first term in the sequence;
`k` is the last term in the sequence.

This formulaic approach is far and away the fastest way to add the sequence.

However, if you do not have the math background to support its use, the don’t use it in an interview or on a test. You’ll be asked how you came to learn this and someone telling it to you is not the answer they want to hear. That you learned it from study of Series and Sequences in academic math is what they want to hear.

It’s for this same reason that I tell people my first rule of coding… Never use code you do not fully comprehend and could not have come up with yourself and explain. One should always endeavor to work out solutions based upon their level of understanding. Write something that works, then study it, refine it, and write it again some other way.

This is not the correct solution. If you reread the question and look at my example you will see that no numbers can be divisible by 5. Check the solution set and see if there are any in there that are divisible.

Can you explain why someone might question this solution and ask where you got it? The usage of range is a dead give away that there is some lack of understanding. Okay for a beginner, but not okay otherwise.

A beginner would not have come up with even that solution because they are not adept at logic yet, and that approach (one I’ve explained many times over the years) is quite intuitive and shows an understanding of Prime Numbers (which are not explained in the problem in the PMP unit). Still, don’t let me criticize if you actually did come up with this on your own. Who am I to doubt? Still, there are flags going up, so you know.

If as you say these exercises are intended to learn what you know, then don’t set the bar too high for yourself or expectations of your teacher will match. Write simple code that a beginner would write, and don’t be afraid to skip a question if it is over your head. Be honest, especially with yourself and you will be very teachable. Any teacher will prefer that to having their expectations dashed by disappointment because someone was trying to paint themselves with a brighter brush. Be ignorant and let the teacher be your guide.

Proof of the above formula

``````s  =  1 +  2 +  3 +  4 +  5 +  6 +  7 +  8 +  9
s' =  9 +  8 +  7 +  6 +  5 +  4 +  3 +  2 +  1
2s = 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10
2s = 90
s = 90 / 2
s = 45
``````

Given,

``````  n = 9
t1 = 1
t'1 = 9
s = n * (t1 + t'1) / 2
``````

We’ll note that t1 and t’1 are the first and last terms of the sequence. Computationally it is faster to multiply integers than it is to multiply floats so the division is left to the last step. (4.5 is a float 4.5 * 10 could take up to 4 times longer to compute.)

But if you haven’t studied this math, then stop right there because what if someone asks, “What is the sum if the common difference is 2, or 3, or 5, …?”? Only if we understand the math can we answer this question. It is not a far reach to say that I learned a lot of math without even knowing it while I learned computing with algorithms. The one above, though, I learned in high school.

Be yourself, without trying to win recognition at this stage. Your eagerness, honesty, willingness to work and learn will be all you need. I think you’ll do just fine.

And I think a teacher would be just as impressed by the naive solution, off the bat…

``````total = 0
for x in range(1, 778):
total = total + x
print total
``````

Never discount the value these algorithms possess. While extremely slow compared to your solution (or mine), they keep us connected to the roots of programming, and how the thinking expanded from there. Don’t miss out on this chapter of the book of learning.

You are right, the first exercise can be simpler than that, like your last example. And it’s more what i have been teached in the chapters. That is quite intimidating to see that a simple exercise can be solved by so many ways.

You are right my second exercise is not correct.

``````i have come up with this:
def Exercise6(x):
for x in range(77, 778):
if not x % 7 and x % 5:
print(x)

Exercise6(777)

``````

Is there a simpler way to print my result ? i am not sure about this but it is finding the good numbers so…

For the last one on prime, i used the code ‘‘Pratices Make perfect’’ : is_prime.

I have analysed this code a lot to understand why he is right.

def is_prime(x):
if x < 2:
return False
else:
for n in range(2, x-1):
if x % n == 0:
return False
return True

print(is_prime(11))

I printed number to verify prime and its correct. But yea its maybe not a beginner approach.

Now push everything aside, including any outside resource, sit down to a blank screen, and do every exercise again, like a test, and write the code from scratch using only your own thinking and logic. Stuck already?

That’s what I mean. Right now you are trying to learn by rote memory and reverse engineering. If you are not writing authentic code of your own, however naive, then you are missing out on the essential learning experience. It’s much easier to debug our own code than someone else’s. A teacher can trap you if you try to pretend. Once caught you can never remove the stain. Be genuine. Be original. Be a beginner with wide eyes and ready mind. Don’t wait for things to stick to you. Think the problem through, visualize an approach, write, test, debug and prove your concepts. Then it becomes a part of you.

But did you, though? That’s not original (from a beginner’s standpoint). What if we haven’t yet learned logical operators? This problem can be solved without them.

``````for x in range(77, 778):
if x % 5 != 0:
if x % 7 == 0:
print (x)
``````

Notice how much that looks like your original? That’s what I mean by naive. It’s what someone who is just getting their bearings would write. It works, right? Work toward this style of writing from the ground up. It’s more original, genuine and honest. If you wish to learn, then be a learner not a voyeur. Better you are surprised by what you write that works, than be content to have figured out why someone else’s works.

Forget the adjectives, better, simpler, etc. Write from scratch, get it to work, put in the effort and enjoy the reward in the end. One can take no satisfaction in a good mark if it is achieved by other means. Six months later you’ll be amazed by how much you have already forgotten.

i have to do the last exercises ( i have done well thanks to your hints and help), and i have trouble on this one:

class Coordinate(object):
def init(self, x, y):
self.x = x
self.y = y

def getX(self):
return self.x

def getY(self):
return self.y

def str(self):
return ‘<’ + str(self.getX()) + ‘,’ + str(self.getY()) + ‘>’

I have to ""Write a class method to check if two Coordinate instances are equal. ".

I have learned through the Introduction to classes but it does not help me to know how two instances method can be equal…They have just to be… identical??

I was thinking maybe use the def eq but i am not sure.