2

I'm solving the following problem:

The assignment is to create and return a string object that consists of digits in an int that is sent in through the function's parameter; so the expected output of the function call string pattern(int n) would be "1\n22\n..n\n".

In case you're interested, here is the URL (You need to be signed in to view) to the full assignment, a CodeWars Kata

This is one of the tests (with my return included):

Test-case input: pattern(2)

Expected:

1

22

Actual: "OUTPUT"

//string header file and namespace are already included for you
string pattern(int n){
    string out = "OUTPUT";
    for (int i = 1; i <= n; ++i){
      string temp = ""; 
      temp.insert(0, i, i);
      out += temp;
    }
    return out;
}

The code is self-explanatory and I'm sure there are multiple ways of making it run quicker and more efficiently.

My question is two-fold. Why doesn't my loop start (even though my expression should hold true (1 <= 2) for above case)?

And how does my code hold in the grand scheme of things? Am I breaking any best-practices?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
geostocker
  • 1,190
  • 2
  • 17
  • 29
  • 2
    What on Earth is a CodeWars kata? You are better off reading a c++ book than wasting your time on those bogus code wars / judges / whatever websites. Banzai! –  Nov 16 '16 at 13:06
  • Haha. CodeWars is basically an interactive learning environment where developers create Katas (exercises) that other developers can solve and have their solutions rated on the website by other people who have solved the Kata. If I'm not completely mistaken a Kata is the collective term for a move or a combination of moves in primarily Japanese martial arts. :) You should check it out! :) – geostocker Nov 16 '16 at 13:08
  • Your link doesn't work, it requires registration. – Agent_L Nov 16 '16 at 13:10
  • 1
    I hope you do realize that I actually had written in the paranthesis BEFORE the link that you have to be signed in to view... :/ – geostocker Nov 16 '16 at 13:11
  • So, it's just advertisement of a 3rd party website? – Agent_L Nov 16 '16 at 13:13
  • @Agent_L No. It's a question which contains enough information to be answerable, and links to more info to a 3rd party site (which happens to require registration). I see no problem here. – Angew is no longer proud of SO Nov 16 '16 at 13:14
  • Of course it isn't... God. It's just there in case someone might want to see the full spec. – geostocker Nov 16 '16 at 13:15
  • 1
    The link is blocked on my network, usually not a good sign. Also, posting links to external websites and expecting people to log in on top of leaving SO is a bit much. And I can't see the environment so I don't know if there is or not, but if there's no debugger, then don't even consider further, just drop the website. – George Nov 16 '16 at 13:15
  • If you want to show us full, working program, then use some public server we can actually view, like `codepad.org`. If you expect only CodeWars people to help you, why don't you use CodeWars community in the first place? – Agent_L Nov 16 '16 at 13:17
  • 1
    As I keep on telling you, I'm not expecting anyone to look at the link. The example I provided with the code should be more than enough for the viewer to understand my problem. If it truly bothers you so much, then I'll take it off. – geostocker Nov 16 '16 at 13:18
  • If you're learning C++, you might want want to follow a [good book](http://stackoverflow.com/q/388242/1782465). – Angew is no longer proud of SO Nov 16 '16 at 13:21
  • If you don't expect anyone to look, then why post the link in the first place? All it does makes you look like you're advertising a service. – Agent_L Nov 16 '16 at 13:21
  • 1
    @Agent_L I tried to edit the Q to de-emphasise the site (but it didn't look like promotion to me to begin with). It can still be useful to provide context, if someone's interested. Do you think it looks OK in its current form? – Angew is no longer proud of SO Nov 16 '16 at 13:22
  • @Angew Thanks for the advice. I've looked at the list. I'm currently going cover to cover in the latest edition of the Primer. But that bible is thick as all hell. Do you have any advice on a good book that gives a lot of exercises with solutions for developers with experience? – geostocker Nov 16 '16 at 13:22
  • @Angew Looks great to me now. – Agent_L Nov 16 '16 at 13:24
  • @geostocker Not really, sorry. I just know the list exists and link to it to spread the knowledge when I think it might be relevant. I have little experience with C++ books in general (I learned the mid-level at university, and the advanced stuff "in the trenches" and by reading the standard). – Angew is no longer proud of SO Nov 16 '16 at 13:30
  • @Angew No worries, mate. I've been looking around for a few days now - but can't find any books that covers c++14 and has loads of good questions AAAAAND is widely recognized as a good book. :P – geostocker Nov 16 '16 at 13:35

3 Answers3

2

The overload of std::string::insert() that you are using takes three arguments:

  • index
  • count
  • character

You are using i as both count and character. However, the function expects the character to be of char type. In your case, your i is interpreted as a character with the code of 1 and 2, which are basically spaces (well, not really, but whatever). So your output really looks like OUTPUT___ where ___ are three spaces.

If you look at the ascii table, you will notice that digits 0123...9 have indexes from 48 to 57, so to get an index of a particular number, you can do i + 48, or i + '0' (where '0' is the index of 0, which is 48). Finally, you can do it all in the constructor:

string temp(i, i + '0');
SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
1

The loop works - but does nothing visible. You insert the character-code 1 - not the character '1'; use:

temp.insert(0, i, '0'+i);
Hans Olsson
  • 11,123
  • 15
  • 38
0

the insert method is not called right: temp.insert(0, i, i); --->

temp.insert(0, i, i+'0');

heming
  • 24
  • 3