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?
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)
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
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?
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))
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
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
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.
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")
# find the no. of digits in a number :)
a=str(abs(int(input("give any integer:"))))
print(len(a)) easy way
factorial: 2:40 3:17 4:00
digits: 7:55 8:50 9:00 9:19 9:30
reverse: 12:20 13:45 15:08 15:22 15:43 16:00 16:30 17:17 20:12 21:07 21:42
palindrome: 25:45 26:28 26:50
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?
Yes I also did same
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)
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
then -1 would give 2. you will have to add a if block to this.
Use abs(len(str(num)))
Easier way to do it:
n=(str(input('Enter a Number:')))
n=n.lstrip('-')
print(len(n))
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?
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?
could also be (0 - rev)
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])))
@@avenumadhav3568thanks for this simple code.
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))
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
print(rev * -1)
can we use this too?
problem-3
n = int(input("Enter a number: "))
num = str(abs(n))
num = num[::-1]
if(n>0):
print(num)
else:
print("-"+num)
WE CAN ALSO SOLVE THE PROBLEM-3:
num=int(input("enter the no."))
rev=str(num)
print(rev[: :-1]
can you please explain the last line?
its a shortcut used in strings to reverse it@@tauxic
As someone new to coding, how to get acquainted with the logic of the code?
i would recommend datacamp. There are python tutorials especially for data science and beginners.
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
20:10 Can't we just add a minus sign before printing rev. Like print(-rev). It seems to work fine.
i have doubt question 3
reverse digit program fails when i entered 0123 and 1230 why? how to rectify it?
No, it shouldn't fail. Perhaps you made some mistakes
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")
Problem 2 i/p 0 Exo/p should be 0, right??
4:49 the code now fails the test case (num = 0)
We can print fact for that case
problem 3 code will not work on 10,20,30,800. Basically number ending with 0 will not work with this code
Well! guess what? it's working. Kindly check before commenting.
@@saunakroychowdhury5990 I have also Checked , But it is not working for me as well
In case of 10 , it only prints 1 , technically it should be printing 01 right ???
12:20 reverse the digits
num = abs(int(input()))
print(str(num)[::-1])
test case for negative fails!!
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))
ye wala sayad work karenga :)
Short answer: -
7:55 number of digits
n=abs(int(input()))
import math
result=int(math.log(n,10))+1
print(result )
More shorter :)
n = abs(int(input ())
b = str(n)
Print(len(b))
Problem 2: digit=digit+1
Digit=1+1=2 please someone explain this
My code literally, for q.2
a=abs(int(input()))
a=str(a)
print(len(a))
Can we use this? Is this Correct?
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.
@@rituparnadas8344 Yes, You're right.
@@rituparnadas8344 No it's not working. We have to change it to string. Just now did it in replit
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
@@rituparnadas8344 The logic is correct but we are talking about numbers so maybe the approach is not correct. Btw, similar names.🤝
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")
Which compiler is being used by Sir ?
spider
problem-3
n = int(input("Enter a number: "))
num = str(abs(n))
num = num[::-1]
if(n>0):
print(num)
else:
print("-"+num)
please let me know if this is a valid piece of code