Same code works on C++ but not in python

To clarify, I got AC with the code I attached after my answer. I’m not sure if int() floors the value or rounds it. So I used integer division.

1 Like

@anon53253486 i used floor then also it was showing partially correct ans
here’s my code
https://www.codechef.com/viewsolution/33814827

1 Like

print(int(39/10))
print(39//10)

I just checked, these two operations give the exact same answer. Python floors division value.

1 Like

int () also floors it

Never use int(a/b)…it gives precision errors.
Always use a//b. I have experienced this.

4 Likes

not always

it doesnt matter bro as i have used int() while printing it floors the input

1 Like

I agree. I have experience as well. The only place I use int() in python is n = int(input()). It is useless otherwise (not always, but for this scenario, definitely).

this made sense i replaced / with //
now i am getting correct answer. my program finally WORKS

but can you plz elaborate why int() doesnt give correct answer

Not necessarily. Always use // for arithmetic operations.

Link 1
Link 2
Link 3

@sachin_01 For very large number int() isn’t that accurate.
E.g.-
print(99997777555533331//2) >>> 49998888777766665
print(int(99997777555533331/2)) >>> 49998888777766664

1 Like

@rahul u r right it happens because when we use / the result is calculated in floating point ,and internally it is stored in hardware optimised IEEE-754 binary64 standard.
/ will give you wrong answer for even smaller inputs further when u use int over it ,it may results in wrong/unexpected answer .
for ex. 10/3
print(int(1.9999999999999999999)) will return 2 instead its floor value that is 1
thats why i use and recommend using // if u want floor value.
if anyone wants complete explanation of IEEE–754 binary64 standard.
plz let me know
@sachin_01 @sid_121212

1 Like

Precision error for 10**18 in dividing

you can use

from decimal import Decimal
print( int( Decimal(ts)  /  Decimal(2) ))

this will work

i used floor only ,check my solution , after that also it was showing partially correct then i submitted the same solution in cpp it worked .
Btw thanks :blush: @faisal1947

1 Like

u r welcome buddy.

try using “//” in line 12 of your solution, it shall work, or have a look at my solution for this question.

Happened to my python code too I tried with floor operator and int type conversion both. Eventually after 5 partially accepted submissions, submitted the code in C++ got 100!