Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

put this into a loop  RSS feed

 
Ranch Hand
Posts: 163
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this reverses a string



but how could i put this into a loop?
 
Marshal
Posts: 63823
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wayne brandon wrote:this reverses a string . . .

No, it doesn't It prints the String in reverse order. Yes, you can put that into a loop; start by writing it on paper with your screen turned off.
 
wayne brandon
Ranch Hand
Posts: 163
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok

what i know:
a string can be indexed 0 to string length eg: string "code" is [0:3]
                                 -1 to -string length (from right to left [-1:-4]

make a for loop
for i in range (start count, to count to, to count by (default is 1?)

what i need to do:
print out a string in reverse

what i think should be done:

i need to create a string
myString = "I love CodeRanch"

create another empty string:
reverseString = ""

get the length of the string
stringLength = len(myString)

get the for loop to take indexes from the original string and put them in reverse to the new string
take the last index from the original string and put it in the first position of the new string
and so on

but i have no idea how t implement that in a loop

i dont even know if im going about solving this correctly




 
wayne brandon
Ranch Hand
Posts: 163
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in this code:



this line:

what is -i in the array doing, i dont really understand this
 
Sheriff
Posts: 13384
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A positive index normally starts from the "left" side of the array or from index 0.  A negative index will indicate an offset from a.length.  I don't know if this makes sense so I'll give you an example:

If you have an array, a, that is 5 elements long with indices from 0-4, then a[0] is the same as a[-5], and a[1] is the same as a[-4], a[2] is the same as a[-3], a[3] is the same as a[-2], and a[4] is the same as a[-1].

That is,

Given s = "ABCDE"

s[0] == s[-5] == 'A'
s[1] == s[-4] == 'B'
s[2] == s[-3] == 'C'
s[3] == s[-2] == 'D'
s[4] == s[-1] == 'E'
 
Ranch Hand
Posts: 67
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another way how you can solve the given problem.

 
Greenhorn
Posts: 2
Android Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this code for reverse string
 
wayne brandon
Ranch Hand
Posts: 163
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
very nice mark

much smaller

can you please please break down this line for me? im just not getting it
print(myString[len(myString) - i-1], end='')

 
Junilu Lacar
Sheriff
Posts: 13384
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What don't you get? How a negative index works has already been explained. I would think you understand how basic arithmetic and addition/subtraction works so if you substitute values and do the math, you should be able to figure out what index values are used. I imagine you know how for loops work by now. And the end='' part just suppresses a line feed. Part of learning how to program is learning how to break down code into its component parts and then reassembling it again to understand how they all work together.

Why not give it another shot yourself and try to explain what your understanding is? That way we can see what exactly you're missing.
 
Junilu Lacar
Sheriff
Posts: 13384
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also,

wayne brandon wrote:much smaller


How is this:

any smaller than this:



If anything, I think the latter is smaller and clearer
 
wayne brandon
Ranch Hand
Posts: 163
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just meant he was using the same string, not creating an empty string ""
then moving things into that.

but yes, i have looked at the code and changed values and experimented with it and understand it a lot better now. Thanks!
 
I'm still in control here. LOOK at this tiny ad!
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!