• 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

codingbat String2 starOut explanation

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ive been practicing on codingbat trying to figure out the problems, and things were going well until I got to the level 2 problems, now I spend a lot of time on one problem and still cant seem to figure it out. Then once I've given up on trying to solve it, and search for the solution so I can hopefully understand how the problem is solved, I don't really understand how all the code is working line by line. So here's the one I am stuck on today, hope you all can help me understand and correct where I'm wrong. (The code works, I just don't fully understand how)

https://codingbat.com/prob/p139564
Return a version of the given string, where for every star (*) in the string the star and the chars immediately to its left and right are gone. So "ab*cd" yields "ad" and "ab**cd" also yields "ad".

 
Marshal
Posts: 65114
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please always tell us what goes wrong with code. Also please supply a link to the original codingbat question, so we shall know what you are supposed to do.
You usually cannot solve that sort of question by writing code. You have to work out what the algorithm will be, starting on paper, and then proceeding to pseudo‑code.
 
Sheriff
Posts: 6128
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should credit the place you found the solution, maybe with a link to it.
I don't "understand" this either, meaning I'm not sure why the author wrote it, but if I were to put the code into a sentence, I would say:

If this is not the first character and the character is '*' and the character to the left of it is not '*', then strip the last character off of the result string.

Does that help?
 
Bartender
Posts: 2330
100
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Replying to an old thread)

This can be solved using a single line of code. I tested my solution and it passed all the tests on that site. Hint : Use Regex with replaceAll. I'll not post the complete solution here.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!