(SPOILERS) Duct Tape - Python (what do you think of this code?) - Beginner | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

(SPOILERS) Duct Tape - Python (what do you think of this code?) - Beginner

#import math h = int(input()) # 10 or 6 w = int(input()) # 5 or 3 hin = h*12 # 120 or 72 win = w*12 # 60 or 36 supin = (hin*win)* 2 # 14400 or 5184 dt = (60*12) * 2 # 1440 tapes = (supin / dt) # = 10 or 4 print(-int(-tapes // 1)) #print(math.ceil(tapes)) ------------------------ I wanted to do it without using any imported modules. It took me a while to properly understand the question of this Code Coach... I first thought we were calculation the perimeter, then I thought we were calculating the superficies of each sides of the door frame, but then I decided to read the question again... carefully this time and realized they were asking to cover the whole door with duct tape (what a strange idea). So what I did was convert everything to inches, find the superficies of the door (*2 for each side) and of the duct tape roll and divide them (door superficies / duct tape superficies). I struggled a little bit to find a way to round up numbers to an integer without using math module... I tried to figure it out, but all my ideas were too complicated for such a simple task. I looked at others users suggestion (on stackflows.. etc..) to round up numbers and I couldn't find anything that was simple enough for me and lots of people were arguing... so I decided to ask ChatGPT how I could round up a float without using the math module. At first, ChatGPT gave me an example with the math module, I had to specify more clearly that I didn't want to use the math module. Then, it explained to me that I could use this code : x = 3.7 rounded_up = -int(-x // 1) print(rounded_up) # Output: 4 and said : "This method works by dividing the float x by 1 (to keep it a float), then applying the int() function to truncate the fractional part (effectively rounding towards zero), and finally negating the result before passing it back to int() to simulate ceiling rounding." and asked the AI to break it down for me. That's where the AI started failing and I had to decipher that bit of code. I'll explain more below.

2nd Apr 2024, 2:41 PM
Karl Breault
Karl Breault - avatar
6 Answers
+ 6
Karl Breault I am impressed by what you did there. It is a brilliant approach! Now that I have seen your elegant idea, I will likely use it, too. Here is a different way that I worked out also to avoid importing ceil(): tapes, partial = divmod(supin, dt) print(tapes + (partial!=0)) The divmod() function is built in. It returns the quotient and remainder as a tuple. If the remainder is non-zero then the print statement adds 1 roll.
2nd Apr 2024, 4:40 PM
Brian
Brian - avatar
+ 6
I think this is a great way to better understand these operators and functions, and an exciting brain exercise to solve problems with a little math. As to the rounding up, here is another idea using a conditional expression: r = int(x) if int(x) >= x else int(x+1) https://sololearn.com/compiler-playground/cncuMPSduEnu/?ref=app Nonetheless, avoiding built-in modules at all costs, is a bad strategy. It is ok to rely on code that was written by others and tested millions of times, before you try reinventing the wheel and make a rookie mistake or miss a corner case. Also, as soon as you understand the task, it is OK to use a bit of math to simplify the formula (even using pen and paper), and only make the machine do the calculations that are really necessary. In case you deal with more complex math, this can save a lot of processing time and ultimately costs too. And the optimized shortcuts are often unavoidable if you care about performance.
2nd Apr 2024, 4:47 PM
Tibor Santa
Tibor Santa - avatar
+ 3
More on the method used to output the rounded up number. Basically, you are using floor division on a negative number because floor division on negative numbers always round to the negative infinity. Here's an example : -5 divided by 2 is -2.5 so rounded to the negative infinity, you would get -3 (toward negative infinity). Also, a division always create a float. " In the expression -int(-x // 1), the negative sign before int and the use of floor division (//) are key to rounding up the float x. When the number is negative, the floor division operation effectively rounds towards negative infinity, which, when combined with the int() conversion and negation, results in rounding up the float." -x // 1: This part of the expression divides -x by 1, which effectively truncates the fractional part of -x and leaves only the integer part. For example, if x = 3.7, -x // 1 would result in -4.0. int(...): The int function is then applied to the result of -x // 1, which converts the float -3.0 to an integer -3. -int(...): Finally, the negation operator - is applied to the integer -3, resulting in 3. (minus+minus=plus) You are assigning a negative to the variable, dividing by 1 the negative value to round it toward negative infinity (round up in negative), converting the float to a integer and the "-int" is also converting the negative number to a positive number (- + - = +). I think it's a really cool way to do it without using modules and I learned new things that I wasn't taught using SoloLearn so far. Let me know what you think of the code below, how it could be improved.. etc..
2nd Apr 2024, 2:55 PM
Karl Breault
Karl Breault - avatar
+ 3
Karl Breault , Whoever created the original copyrighted code that got scraped by the people training their commercial AI product without the creator's permission and without giving them credit let alone royalties, which the AI later regurgitated, had a good idea. And if you want it to stay a float, you can just do this. -(-x // 1)
2nd Apr 2024, 4:46 PM
Rain
Rain - avatar
+ 3
Karl Breault , Not the same, but related, a trick from my C=64 days, how to round to the nearest int if all you have is int truncation: Add 0.5 first (rewritten in Python). n = 5.4 print(int(n + 0.5)) # 5 n = 5.5 print(int(n + 0.5)) # 6 n = 5.6 print(int(n + 0.5)) # 6
2nd Apr 2024, 7:06 PM
Rain
Rain - avatar
+ 1
This is so brillant, may i use it for an code
3rd Apr 2024, 7:47 AM
🇸🇾 Abdulaziz Alnaser 🇸🇩
🇸🇾 Abdulaziz Alnaser 🇸🇩 - avatar