Python: Reversing ints ie 432 into 234
I need to reverse a number like 876 into 678. Also is there a way to take a list like:
[122, 123, 124, 663, 808, 777] and weed out the repeats(122, 663, 808, 777) I was thinking a for loop would work but I don't know exactly how to do it. Thanks |
What programming language are you writing this in?
unfortunately, its not that simple. You would need to change the int into something else, and then rearrange, and convert it back into an int. Its difficult to help when i dont really know what language your using, because there are different methods. If you had the number as a string, you could write a nice little for loop to reverse the character order, and then convert it into an Integer. As for the best way of making a list, it depends what your trying to do. You could just slap the values into an array (if thats feasible) or you could try making use of a Linked List data structure (for example) |
I'm not very familiar with Python, but here's a possible way of doing all that:
Reversing numbers: Convert number to string, reverse string, parse string back into integer. Weeding out repeats: Again, convert number to string, then using a loop move through each character and compare to each character in the string (again a for loop). If any are the same, then flag it and get rid of it from the list. These aren't the best possible solutions, but that's the first thing that came to my mind. Again i'm not exactly a pro in Python, but i can provide you with a c++ or java version. Hope it helps. |
Err Im using Python just like it says in my topic. :D I will look into that
coolman0stress. Thanks |
im new to python too, so here goes -
[12, 34, 56, 78, 89] gives [21, 43, 65, 87, 98] and, [123,345,567,678] gives [321, 543, 765, 876] if thats what ur lookin for, i did a Code:
map (rev.rev, [12, 34, 56, 78, 89]) Code:
def rev(x): Code:
if x != NULL as for weeding out duplicates, look at operations on tuples. it has these 2 - count(), and index() . find the number of occurrences with count, for 1 less than that count, remove the occurrence at index. i think its prolly a line or two more...but u can do that. hope that helps. |
Likewise, an alternative (recursive) function:
Code:
#!/usr/bin/python Code:
if x != None: |
Quote:
if not i really am going insane :( (edit - if you do happen to need either a C or Java version im sure i can send one your way :P) |
fr0zen, your code works well but if I reverse say 980 I get 89. This effects my code.
Code:
# Solves the Gym Puzzle Help is wanted because my teacher gave us over the weekend to do this and its due on monday. Its not for a grade but I told this kid that I could do it and I will lose face if I can't(arr my big mouth) I thought it would be simple but you know how dificultes spring up in programming) Thanks |
Quote:
|
Tesla, its the problem with the zero at the right side.
190 and 19 are NOT the same. |
I finaly got it done and it looks like a winner!
Code:
# Solves the Gym Puzzle |
what kinda school has a gym teacher giving math problems to solve in python? lol. and on top, promises beer?
hellloooo - i wanna join this school of yours. :) |
All times are GMT -5. The time now is 11:14 AM. |