HW1: Rounding Numbers

Is it possible to solve this question without using any if statement or any other built-in Python function?
Maybe the question will be more accurate if it was, Is it correct to solve the question using methods not taught yet?


asked 23 Feb '12, 05:32

Albert%20Alegria%20Gendre's gravatar image

Albert Alegr...

accept rate: 0%


Solved thinking a little. Thanks

(23 Feb '12, 05:59)

Albert Alegr...

Albert%20Alegria%20Gendre's gravatar image

33 Answers:

1234next »


It is possible to solve, i.e. using mathematics. You have to do basic arithmetic since we've had the basic operations it is solvable without using for loops and if conditions.


answered 23 Feb '12, 05:41

implicit_knowledge's gravatar image


I think there are even slightly different ways of thinking of this problem and achieving the rounding without using things not taught in class yet. It's more challenging to restrict oneself to not to use the round function or if statements. After the submission deadline the different answers should be discussed in one place in the forum - I'm not sure if it was appropriate as answers to this question or to start a new question ┬╗Solutions to HW 1 #9┬ź then and discuss different solutions there?

(24 Feb '12, 02:50)

sebastian my...

sebastian%20myname's gravatar image

Why do you guys post the answer here???!! Do you gain extra credits/points for it?
I've submited my answer before coming here and then I found that the answer was here!? Come on!!
One thing is letting people be aware that it can be solved with only the things that were taught in Unit 1, the other thing is saying how to solve it!!
Let people think. That is the only way we can really learn!!

Really angry ...


answered 23 Feb '12, 20:41

cbandarrinha's gravatar image



Are you responsible on how people learn? Obviously, many community members are having problems with this question. The problems vary from knowing a little math (adding the right number) to python programming. The community consists of many types of users... noobies being one. They come here for help. Tips, spoilers, or whatever... its the users choice, and Udacity's choice to remove Tips, spoliers, or whatever. People learn differently. There is no monopoloy on how people should 'REALLY' learn.

(23 Feb '12, 22:41)


dedos's gravatar image

@dedos, the only problem is that posting answers is against the TOS. Point 3 states that:

(students) will not share content or solutions to homework assignments or exams

(23 Feb '12, 22:45)


Fuzzmz's gravatar image

@Fuzzmz, ... and Udacity will take care of delineating where the line is drawn. It appears that 'content' and 'solutions' vary in definintion as this thread shows.

(23 Feb '12, 22:53)


dedos's gravatar image

At the risk of repeating myself, "it can be done" is not a helpful answer when someone is stuck. If we weren't supposed to discuss useful approaches to problems, these forums wouldn't be all that useful.

(24 Feb '12, 01:34)

Avrila Klaus

Avrila%20Klaus's gravatar image

But to quote from a reply @PeterUdacity made when I commented that I wasn't sure how much help was appropriate:

"Try to do your best to clarify the problem, but avoid talking about how you would answer it. I know this is vague. I'll talk it over with Professor Evans later."

Also, keep in mind that they have said in multiple places with respect to the last homework question that they do not expect us all to be able to do it. They will post an answer and explanation, and we can all discuss and re-explain all we want, once the deadline has passed.

I know it's a really hard line to draw, and very frustrating not to be able to help. But I think a lot of the discussion of this problem has gone well beyond the "clarify the problem" and into the "how you would answer it" territory.

(as an aside, and a defense of @cbandarrinha, when this answer was first posted there were a couple links, including one in this thread, to an actual solution hosted at pastebin. They've since been removed. But that seemed clearly over the line.)

edit to add: from my perspective, when I was stuck because I was convinced that I needed either conditionals or int(), posts that said "it's possible to do it without either of those things" were an absolutely perfect hint. They reassured me that continuing to think about it wasn't a total waste of effort, but still forced me to come up with the solution on my own. And with several days before the homework is due!

(24 Feb '12, 01:41)

Lisa McLean

Lisa%20McLean's gravatar image

@Lisa... A user makes an active choice to 'click' on a link which they know they can get the answer. If you are a student that has already solved the problem then it's good to compare your answers with others. If you are a student that is stuck, doesn't care or is fed up of waiting, the choice to click on the link is still yours and you'll learn something (hopefully).

This is a very vague/grey area as @PeterUdacity has pointed out. If they define the rules to strictly they would have to police the site constantly looking 'rule violators'. If they define the rules to vaguely... well that's why we're here.

Some of us think that some users are sharing too much information and others are grateful for the posts. We all approach and solve problems very differently. @Lisa has great self-motivation to continue moving on and pushing forward after being reassured... others don't have that or haven't developed it yet. Either way I think what @Avrila Klaus has done is more beneficial than delimiting.

If many feel that these types of posts are sharing 'too' much information then perhaps a special tag that is announced with the question that states: 'Some users feel that TOO much information is being shared about solving the problem in this thread... Enter at your own Learn-isk'. The announcement would deter those self-motivated users who do not want too much information in solving the problem (active choice). The announcement would also bring in those users having the most difficulty.

The choice is and always will be yours.

(24 Feb '12, 03:12)


dedos's gravatar image

Well, that took me longer than expected, but managed to do it with just addition, assignment, find, str and string locations. Convoluted as hell, I'll admit, but alarmingly proud of myself - the only thing that would have been helpful in the instructor comments was a list of what stuff we "know" at this point (or perhaps a statement that we don't yet know conditionals or modulo (%)).


answered 23 Feb '12, 06:08

Jamie%20Sawyer's gravatar image

Jamie Sawyer

Try scanning through the unit notes in supplemental materials. That's how I found everything we 'know' from the course already.

(23 Feb '12, 17:48)

David Harris

David%20Harris's gravatar image

For a little extra challenge, try solving it without any variable assignments.

(23 Feb '12, 20:58)


pseudonoob's gravatar image

I'm trying to state this vaguely enough to be a nudge rather than a spoiler but I'll understand if someone thinks it's too much.

The trick is to NOT try to program the rounding process we learned in grade school. Instead, look at the next integer up as a finish line, and do something to the number you start with, so that numbers which round up will cross the finish line, but numbers which round down will not.


answered 23 Feb '12, 17:48

Avrila%20Klaus's gravatar image

Avrila Klaus


That's pretty much giving the answer away.

Some say this can be done in 1 line of code, and they are right. However converting a number to a string is pretty inefficient and should be avoided performing twice if possible.

(23 Feb '12, 19:50)

David Zhang

David%20Zhang's gravatar image

I'm really not trying to give it away. The thing is, I was getting pretty frustrated with answers like "it's really easy once you see it" when I was trying to get some pointers. My goal was to indicate a direction to go in so that someone who hadn't gotten the flashbulb moment yet would be able to make the final leap themselves...and of course it's going to look like a giveaway to someone who knows what I'm trying to indirectly get at, since they have that frame of reference.

(23 Feb '12, 20:49)

Avrila Klaus

Avrila%20Klaus's gravatar image

Ahhhhh......the slightly convoluted way (programming the rounding process) by which I attained the answer now seems pretty dumb. Although, I did manage without conditionals.

(24 Feb '12, 00:36)


kevin-13's gravatar image

Wow, I feel exceptionally stupid now - I have a masters degree in maths, and didn't spot this - I utilised the possible values of find on a one-character string to implement, which as I posted in my answer, was annoyingly convoluted!

(24 Feb '12, 11:59)

Jamie Sawyer

Jamie%20Sawyer's gravatar image

Hi everybody! ;)

It's so simple! I took about 2 hours thinking how to solve this without using "if" or any extra functionality we didn't learn from Unit 1, but finally got the answer.

HINT: Don't think big. Numbers are composed by numbers, just that simple! ;-)


answered 23 Feb '12, 15:04

GorkaMM's gravatar image


I'm just wondering, do we have to consider all decimal places for rounding, or only the first digit after the decimal place?


answered 23 Feb '12, 06:16

AdrAs's gravatar image



only the first digit.

0,1,2,3,4 = 0

5,6,7,8,9 = +1

(23 Feb '12, 06:30)


KenavR's gravatar image

Alright, then I got the right solution :)

(23 Feb '12, 06:39)


AdrAs's gravatar image

Btw, thanks to my whiteboard helping me to solve the issue :D


answered 23 Feb '12, 06:40

AdrAs's gravatar image


Its awesome problem. I am happy to invest my time for solving this problem.

No string found really helps.


answered 23 Feb '12, 10:35

Shuvankar%20Sarkar-4's gravatar image

Shuvankar Sa...

I did it in a one liner..... my brain is wired to optimize sry :)


answered 23 Feb '12, 11:25

Raath's gravatar image


Nice,same here ;)

(23 Feb '12, 20:03)


AlvarHanso's gravatar image

Very easy, hint: convert from number to string, and get the position 0. Good luck


answered 23 Feb '12, 15:43

Nguyen%20Kha%20Chuong's gravatar image

Nguyen Kha C...


What about if the number has more than one digit? Example: x = 2367.1466789. Then position 0 only gets "2"

(23 Feb '12, 15:51)


GorkaMM's gravatar image

@GorkaMM: I used find to get the location of the decimal point and assigned it to decimal, and then used [:decimal] to get everything before it.

(23 Feb '12, 16:19)

Avrila Klaus

Avrila%20Klaus's gravatar image

what about rounding up if required also?

(23 Feb '12, 16:19)


DJOldskool-1's gravatar image

@avrila-klaus Yeah, that's the way it should be done! I was just trying @nguyen-kha-chuong to see that way isn't the best. @djoldskool This is just a part of the whole process. You must do some more stuff to get it to work ;-)

(23 Feb '12, 16:39)


GorkaMM's gravatar image

@Nguyen Kha
This is incorrect

(27 Feb '12, 14:30)

Ahmad AbdArr...

Ahmad%20AbdArrahman's gravatar image
Your answer
Question text:

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported



Asked: 23 Feb '12, 05:32

Seen: 2,937 times

Last updated: 08 Apr '12, 22:38