In problem no.3 why cant we just multiply 'rev' by negative one (-1) to get the final result instead of subtracting two times 'rev' from 'rev'? Is there anything wrong in doing so? Is it not a normal convention? Am I missing here something?
@storiesshubham41452 жыл бұрын
Yes I also did same
@saunakroychowdhury5990 Жыл бұрын
No, you are correct. In this, he made it more complex than necessary. Do it once, and if the input number was less than 0, rev *= -1 (end of if) print(rev)
@meriid70173 ай бұрын
Umm, in palindrome case, we just have to print 'Palindrome' or 'Not Palindrome'. So, can't we just drop the negative case and just go by the absolute (+ve) value as we don't have to bother the negative sign for checking palindrome?
@shreyapal61113 жыл бұрын
In Problem 3 [Reverse the digits in the given number], why did we use _print(rev - 2 * rev)_ instead of _print(-rev)_ to print the negative of rev?
@bhaskartrivedi31143 жыл бұрын
could also be (0 - rev)
@avenumadhav35683 жыл бұрын
can we also not use while loop and do it? num = int(input()) s = str(abs(num)) if num>=0: print(int(s[::-1])) else: print(int('-'+(s[::-1])))
@VENKATAKRISHNANG-h7c Жыл бұрын
@@avenumadhav3568thanks for this simple code.
@1nsh Жыл бұрын
In question number 2 we could have also done ---> num = int(input("Enter the number :")) /n (in next line) print(len(str(num))) This would have been way easier than the solution tought in this lecture
@NeetSheth Жыл бұрын
then -1 would give 2. you will have to add a if block to this.
@ShubhamKumar-ty6ly Жыл бұрын
Use abs(len(str(num)))
@saumilbhatkar26946 ай бұрын
Easier way to do it: n=(str(input('Enter a Number:'))) n=n.lstrip('-') print(len(n))
@SamparkLakshya17 күн бұрын
what about num = int(input('Enter a number: ')) num_string = str(num) length = len(num_string) if num >= 0 : print(length) else : print(length - 1)
@bharathraghu91952 жыл бұрын
As someone new to coding, how to get acquainted with the logic of the code?
@ABHARNAHRAJARAMMOHAN2 жыл бұрын
i would recommend datacamp. There are python tutorials especially for data science and beginners.
@saunakroychowdhury5990 Жыл бұрын
logic is a natural thing. first you brainstorm and try at least something, if you fail, think harder, if you still fail, look for solution. Learn thoroughly, repeat. Eventually, it will come naturally
@vjickstra3 ай бұрын
20:10 Can't we just add a minus sign before printing rev. Like print(-rev). It seems to work fine.
@TheUnknownU3 жыл бұрын
print(rev * -1) can we use this too?
@05-ajitkulkarni88 Жыл бұрын
WE CAN ALSO SOLVE THE PROBLEM-3: num=int(input("enter the no.")) rev=str(num) print(rev[: :-1]
@tauxic Жыл бұрын
can you please explain the last line?
@bhaviniee11 ай бұрын
its a shortcut used in strings to reverse it@@tauxic
@nitinfaldoliya65620 күн бұрын
here - will also be shifted in last
@sarthaksingh6013 жыл бұрын
4:49 the code now fails the test case (num = 0)
@rohinibharti44633 жыл бұрын
We can print fact for that case
@umarulf2 ай бұрын
i have doubt question 3
@TKKARTIK-t4d11 ай бұрын
We can use this as well for reversing of the digits of the number for both postive or negative just using if & elif n = int(input("Enter the number:", )) if(n>0): str_ = str(n) print(str_[::-1]) elif(n
@learnwithrr12302 жыл бұрын
reverse digit program fails when i entered 0123 and 1230 why? how to rectify it?
@saunakroychowdhury5990 Жыл бұрын
No, it shouldn't fail. Perhaps you made some mistakes
@arifinonim46778 ай бұрын
problem 3: n = int(input("Enter a number: ")) num = abs(n) rev = num % 10 num = num // 10 while(num > 0): r = num % 10 num = num // 10 rev = rev*10 + r if(n >= 0): print(rev) else: print(rev * (-1))
@reddygopichand20022 жыл бұрын
12:20 reverse the digits num = abs(int(input())) print(str(num)[::-1]) test case for negative fails!!
@raj_patel2 жыл бұрын
num = int(input()) if(num == 0): print(0) elif(num > 0): print(str(num)[::-1]) else: num = abs(num) print((int(str(num)[::-1])) * (-1))
@raj_patel2 жыл бұрын
ye wala sayad work karenga :)
@steveraphaelpulikottil57552 жыл бұрын
Problem 2 i/p 0 Exo/p should be 0, right??
@SOUMAYGUPTA246 ай бұрын
problem-3 n = int(input("Enter a number: ")) num = str(abs(n)) num = num[::-1] if(n>0): print(num) else: print("-"+num)
@reddygopichand20022 жыл бұрын
Short answer: - 25:45 palindrome n=abs(int(input())) p=str(n) r=p[::-1] rint=int(r) if (n==rint): print("palindrome") else: print("not palindrome")
@AryanGupta-eb2tp Жыл бұрын
problem 3 code will not work on 10,20,30,800. Basically number ending with 0 will not work with this code
@saunakroychowdhury5990 Жыл бұрын
Well! guess what? it's working. Kindly check before commenting.
@piushdas6188 Жыл бұрын
@@saunakroychowdhury5990 I have also Checked , But it is not working for me as well
@piushdas6188 Жыл бұрын
In case of 10 , it only prints 1 , technically it should be printing 01 right ???
@CherryIITM3 жыл бұрын
My code literally, for q.2 a=abs(int(input())) a=str(a) print(len(a)) Can we use this? Is this Correct?
@rituparnadas83442 жыл бұрын
Only the following is enough: a = input() if a[0] == '-': print(len(a) - 1) else: print(len(a)) You don't have to convert the input into an integer and then into a string, because in Python the input is by default a string. P.S. Edited the code, I hadn't considered negative numbers before.
@CherryIITM2 жыл бұрын
@@rituparnadas8344 Yes, You're right.
@storiesshubham41452 жыл бұрын
@@rituparnadas8344 No it's not working. We have to change it to string. Just now did it in replit
@storiesshubham41452 жыл бұрын
This will not work for negative numbers. -2 is giving length=2 which is clearly false. We can take the length of abs(n) and then add 1 to length if n
@rituparnodhar62652 жыл бұрын
@@rituparnadas8344 The logic is correct but we are talking about numbers so maybe the approach is not correct. Btw, similar names.🤝
@reddygopichand20022 жыл бұрын
Short answer: - 7:55 number of digits n=abs(int(input())) import math result=int(math.log(n,10))+1 print(result )
@Dexter4o42 жыл бұрын
More shorter :) n = abs(int(input ()) b = str(n) Print(len(b))
@ranjeet_sohanpal2 жыл бұрын
Which compiler is being used by Sir ?
@vishalmadhesia8842 жыл бұрын
spider
@selvamselvam79602 жыл бұрын
Problem 2: digit=digit+1 Digit=1+1=2 please someone explain this
@jussstdoiit3 ай бұрын
question no 4 easily is num = (int(input("enter your number: "))) int= abs(num) int = (str(int)) if int == int[::-1]: print("Palindrome") else: print("NOT a palindrome")
@SOUMAYGUPTA246 ай бұрын
problem-3 n = int(input("Enter a number: ")) num = str(abs(n)) num = num[::-1] if(n>0): print(num) else: print("-"+num)
@SOUMAYGUPTA246 ай бұрын
please let me know if this is a valid piece of code