Posts: 20
Threads: 4
Joined: Sep 2023
(Sep-07-2023, 02:47 PM)DPaul Wrote: (Sep-07-2023, 01:43 PM)PythonBoy Wrote: I am interested in this method also. Could you please explain why did we write y = x[::-1]?
Look here:
https://www.guru99.com/1-in-python.html
Paul
I understood now, so we used it in order reverse the input, which is a way easier and shorter method to use
Thanks, I'll definitely keep this is mind
Posts: 1,094
Threads: 143
Joined: Jul 2017
Python has reversed() which will reverse a string, but you need ''.join().to get the actual reversed string.
num = 'x'
mun = 'y'
while not num == mun:
num = input('Enter a number ... ')
print(f'Using \'\'.join(reversed(num)) to reverse the number ... ')
mun = ''.join(reversed(num))
if num == mun:
print(f'{mun} reversed is the same as the starting number {num}')
else:
print(f'{mun} is not the same as {num}') I found this way of doing this, which is neat, but creates strings, which is inadvisable I'm told.
num = 'x'
mun = 'y'
while not num == mun:
mun = ''
num = input('Enter a number ... ')
for n in num:
mun = n + mun
Posts: 4,653
Threads: 1,496
Joined: Sep 2016
(Sep-07-2023, 01:43 PM)PythonBoy Wrote: I am interested in this method also. Could you please explain why did we write y = x[::-1]? which aspect of this are you asking?
1. why assign an extra variable y?
2. what does x[::-1] mean?
i would have written it with one statement instead of two:
x = '1234321'
if x == x[::-1]:
print('The number is a palindrome')
else:
print('Number is not palindrome') is that what you were wondering about?
but this has no loops, as your assignment requires. to do it with at least one loop, i would compare the individual characters, one by one, over both ends of the string, until everything has been compared. if any comparisons are unequal then it is not a palindrome. you need to known the length of the string to do this right. i assume you have learned how to get that. you want to have a loop that has two index variables, one for the front, and the other for the back. you compare the front one character to the back one character in the string. i assume know how to do that. if they are unequal, the loop needs to end, right? but if they are equal then it goes on. where does it go and and how does it get there?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 20
Threads: 4
Joined: Sep 2023
(Sep-10-2023, 11:13 PM)Skaperen Wrote: (Sep-07-2023, 01:43 PM)PythonBoy Wrote: I am interested in this method also. Could you please explain why did we write y = x[::-1]? which aspect of this are you asking?
1. why assign an extra variable y?
2. what does x[::-1] mean?
i would have written it with one statement instead of two:
x = '1234321'
if x == x[::-1]:
print('The number is a palindrome')
else:
print('Number is not palindrome') is that what you were wondering about?
but this has no loops, as your assignment requires. to do it with at least one loop, i would compare the individual characters, one by one, over both ends of the string, until everything has been compared. if any comparisons are unequal then it is not a palindrome. you need to known the length of the string to do this right. i assume you have learned how to get that. you want to have a loop that has two index variables, one for the front, and the other for the back. you compare the front one character to the back one character in the string. i assume know how to do that. if they are unequal, the loop needs to end, right? but if they are equal then it goes on. where does it go and and how does it get there?
I was asking what x[::-1] is. And they helped me figure it out.
Yes that is exactly the way I tried doing it but I got an error. Then I got to know that I use an outdated version of python
When I downloaded the new one, my code worked properly
Thank you!
Posts: 1,358
Threads: 2
Joined: May 2019
You mentioned that this is supposed to use loops. The slicing technique is better and faster, but must consider what the instructor is wanting to teach you. In this case, how to reverse a number (or string) using loops, and I would guess that the key instructional point is the use of a negative step value in a for loop. Have you tried
1. Convert integer into a string
2. Loop over the string in reverse (using step value of -1) to create your reversed string
3. Compare to original string
If this were not homework then the solutions of using slicing, reversed(), etc are great. In this case I bet your instructor wants you to reinvent the wheel!
Posts: 20
Threads: 4
Joined: Sep 2023
Sep-11-2023, 02:36 PM
(This post was last modified: Sep-11-2023, 02:37 PM by PythonBoy.)
(Sep-11-2023, 12:21 PM)jefsummers Wrote: You mentioned that this is supposed to use loops. The slicing technique is better and faster, but must consider what the instructor is wanting to teach you. In this case, how to reverse a number (or string) using loops, and I would guess that the key instructional point is the use of a negative step value in a for loop. Have you tried
1. Convert integer into a string
2. Loop over the string in reverse (using step value of -1) to create your reversed string
3. Compare to original string
If this were not homework then the solutions of using slicing, reversed(), etc are great. In this case I bet your instructor wants you to reinvent the wheel!
Actually, good that you mentioned, I think this is the right and the easiest way my instructor wants me use. Because he gave us this assignment after teaching us about range(start,stop,step).
I will make sure to try using this method also
Thank you!
"He who conquers himself is the mightiest warriors" - Marcus Aurelius
Stay Positive, Work Hard!
Posts: 4,653
Threads: 1,496
Joined: Sep 2016
i think the teacher wants you to do a little bit more than stepping an index downward. he/she wants you to be able to index both upward from the start and downward from the end at the same time. that and index the same string both ways.
do you need one loop or two loops to do that? he/she wants you to figure that out for your code.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 6,809
Threads: 20
Joined: Feb 2020
Sitting at the keyboard is not the right way to start programming. First you solve the problem, then you code the solution.
Is 1234521 a palindrome
Output: 1_____1: First and last digits match.
_2___2_: Second and second to last digits match.
__3_5__: Third and third from last digits do not match. Not a palindrome
.
If we save the number as a str, we can use indexing to do the test.
number = "1234521"
number[0] == number[6]
number[1] == number[5]
number[2] != number[4]
number is not a palindrome We can use the index at the start of the string to calculate the index for the end of the string.
number = "1234521"
numlen = len(number)
number[0] == number[numlen-1]
number[1] == number[numlen-2]
number[2] != number[numlen-3]
number is not a palindrome The ending index == numlen - start_index - 1.
Now it should be easy to write a loop to do the test. You only have to loop through half the digits because you are comparing the first half of the number to the mirror of the second half. If you reach the middle without finding a mismatch, the number is a palindrome.
Posts: 20
Threads: 4
Joined: Sep 2023
(Sep-11-2023, 07:24 PM)deanhystad Wrote: Sitting at the keyboard is not the right way to start programming. First you solve the problem, then you code the solution.
Is 1234521 a palindrome
Output: 1_____1: First and last digits match.
_2___2_: Second and second to last digits match.
__3_5__: Third and third from last digits do not match. Not a palindrome
.
If we save the number as a str, we can use indexing to do the test.
number = "1234521"
number[0] == number[6]
number[1] == number[5]
number[2] != number[4]
number is not a palindrome We can use the index at the start of the string to calculate the index for the end of the string.
number = "1234521"
numlen = len(number)
number[0] == number[numlen-1]
number[1] == number[numlen-2]
number[2] != number[numlen-3]
number is not a palindrome The ending index == numlen - start_index - 1.
Now it should be easy to write a loop to do the test. You only have to loop through half the digits because you are comparing the first half of the number to the mirror of the second half. If you reach the middle without finding a mismatch, the number is a palindrome.
This is exactly what I wanted to do. Comparing the first and last digit and so on.
So I made use of loops and it worked. Thanks!
"He who conquers himself is the mightiest warriors" - Marcus Aurelius
Stay Positive, Work Hard!
|