r/badcode • • Mar 10 '21

c++ My beautiful function for getting rid of punctuation from a string from my data structures class. I probably was so proud of this when I wrote it too.

Post image
1.6k Upvotes

181 comments sorted by

274

u/CeldonShooper Mar 10 '21

Management be like: why should we change this? It works, right?

161

u/[deleted] Mar 10 '21

I mean I think I got like a 90 on the program so I meannnnn

30

u/andresq1 Mar 10 '21 edited Mar 10 '21

Bill Feild at FIU would have reported you to the dean

25

u/[deleted] Mar 10 '21

Tbh I would have deserved it

27

u/BruceGrembowski Mar 10 '21

TBH, I once turned in an assignment where one of the user functions didn't work, but I still got full marks. I think graders just get overwhelmed at some point. But your code works, and that's the most important part usually.

16

u/[deleted] Mar 10 '21

The graders this class mostly took into account that you wrote your custom data structures class correctly (like we had to write custom vector class, custom string class, etc.) and that it performed the tasks given on the handouts. Your code being convoluted is not a factor in the rubric, but I honestly should have gotten at least reprimanded for this cursed code

6

u/HoppouChan Mar 10 '21

If it's one of those very basic exercises with an entirely different goal, I also wouldn't have docked points to be fair.

Like, a comment that this is horrifically inefficient sure. But in an intro class not so much

8

u/Blyatman72 Mar 10 '21

What do you mean?

35

u/[deleted] Mar 10 '21

It was for a class and he got 90% on the assignment.

3

u/Blyatman72 Mar 10 '21

Oh ok. Nice

0

u/[deleted] Mar 10 '21

[deleted]

7

u/[deleted] Mar 10 '21

I respect that - I obviously have come super far from these projects but I’d like to think they gave me general conceptual knowledge for technical interviews.

33

u/[deleted] Mar 10 '21

developer be like "I'm getting paid by lines of code, so..."

14

u/tux_unit Mar 10 '21

Or "I'm getting paid by logical branches"

6

u/Aphix Mar 11 '21

Pashaw, we're engineers here, the term you're looking for is, "Cyclomatic complexity'

7

u/Rattus375 Mar 10 '21

I mean it probably doesn't matter. It's about as inefficiently as this can be done (unintentionally), but since the only use case for removing punctuation would be for short strings anyway, this wouldn't acually have an appreciable impact to runtime

5

u/spanishgum Mar 10 '21

At least it’s O(N)!

8

u/naverag Mar 10 '21

Isn't it O(n2) in the worst case (assuming string.remove is O(n))?

3

u/spanishgum Mar 10 '21

Oh wow you’re right! Totally overlooked that 😅

1

u/Garegin16 Jun 19 '21

Management usually listens to expert opinion. Everything is not just about short term bottom line. There are things like security risks and technical debt that could affect future performance.

251

u/helliash Mar 10 '21

Don’t get me wrong, this is a bad code. But look at it from the perspective of reading. It’s kinda haiku. It has rhythm, constant structure, not too long or short, quite a poetry. And as many modern poetry useful just a little bit.

70

u/[deleted] Mar 10 '21

I’ll throw it in some art competition and talk about how it’s symbolic of how everyone forgets how much work is going on behind the scenes in programming. It’s written purposeful bad and convoluted as an ode to the amount of work happening behind the scenes

13

u/helliash Mar 10 '21

Cool, so what do you think? Oil painting? Or even better, printed on needle printer paper with tracks on margins. Man, I would totally buy it.

11

u/[deleted] Mar 10 '21

The code kinda looks like wonky train tracks so I’d go with tracks. Idk though I’d need an artists perspective for sure

68

u/klekaelly Mar 10 '21

How does one get into a data structures class and not recognize how bad this is...?

62

u/[deleted] Mar 10 '21

When one was raised in a small private Lutheran school and didn’t get real CS experience till college 😅

14

u/klekaelly Mar 10 '21

Did you pass?

29

u/[deleted] Mar 10 '21

Sure did! There were some growing pains but I love this field so I fought to catch up

3

u/JollyGreen615 Mar 11 '21

Data structures is like the second class I took for my CS major. I didn’t know anything before that class

1

u/champwolverine Jun 04 '21

When I took Data Structure, I didnt know how nested loops works.

33

u/sixtyfifth_snow Mar 10 '21

Wow, it's a 14-pass solution 😂

16

u/[deleted] Mar 10 '21

Freshman year me was really trying to have my functions have the worst time complexity

6

u/pitolaser Mar 10 '21

Well AKCHUALLY the complexity doesn't change with the number of times you call a function. Yes, it's obnoxious to point that out and I hate myself for it.

8

u/tux_unit Mar 10 '21

Exactly. Some O(n2) algorithms are really just Ω(n2).

2

u/bric12 Mar 10 '21

We could fix that, just make the "while()"s nested...

17

u/Lyri3sh Mar 10 '21

This feels like "your assignment must contain more than xxx words" ...

15

u/[deleted] Mar 10 '21

“Write no less than 2000 lines of code for this assignment”

39

u/HerrNilsen- Mar 10 '21

Ever heard of regex

31

u/[deleted] Mar 10 '21

Only in theory - we discussed it in my general programming languages class but never implemented anything with it

11

u/YmFzZTY0dXNlcm5hbWU_ Mar 10 '21

Assuming you still code (or even manipulate text with find/replace), knowing regex is a game-changer and easier than it looks at face value.

Next time you have 15 minutes to kill watch this and you'll know how it works (at least the basics): https://www.youtube.com/watch?v=bgBWp9EIlMM

Beyond that I also recommend regex101.com to play around with expressions/practice with quizzes, and regex crosswords are a lot of fun too.

6

u/[deleted] Mar 10 '21

Thanks for the resources! Id actually like to learn more about regex considering my professor that taught us it really didn’t give a crap and only talked about it in theory

3

u/BruceGrembowski Mar 10 '21

Just don't try to parse HTML with regex 😁

2

u/[deleted] Mar 10 '21

Noted lol

4

u/YmFzZTY0dXNlcm5hbWU_ Mar 10 '21

Same here, I just watched that video one day out of curiosity and I was hooked. Every time somebody mentions that they don't know regex I whip out that link haha

6

u/tux_unit Mar 10 '21

OH MY FUCKING GOD WHAT COLLEGE DID YOU GO TO

5

u/[deleted] Mar 10 '21

To be fair this code is from my first data structures project I learned how to actually code in that class and would never write something so horrendous again. However my prof for programming languages kinda just didn’t care so I didn’t really learn a lot there :c

6

u/tux_unit Mar 10 '21

No, I'm sorry, I meant that you didn't implement regular expressions. Am I just an old fuck now?

3

u/[deleted] Mar 10 '21

Ah I didn’t even know about regex at this point in time. We also barely covered it in my programming languages class, so I’m learning a lot more about it from this comment section than my class so that’s kinda sad

17

u/notreallyaredditor8 Mar 10 '21

I had the same kind of shitty code when I was implementing an authentication console app for school. Then I learned about regex. It will probably turn this code into a single line

15

u/Rigatavr Mar 10 '21

In c++ you'd have to use an external library, because std::regex is more painful then this image.

15

u/HerrNilsen- Mar 10 '21

Parry this you filthy casual

Edit: u/u/_Syd_knee_ just in case you still need this

4

u/[deleted] Mar 10 '21

I appreciate the info link, I’m learning through this subreddit that my programming languages Professor has failed me

9

u/zacharypamela Mar 10 '21

std::regex(R"([,!.?:;"/\\\\\/'\[\]\\{\\}]+)")

I was going to ask why you had the parentheses around the square brackets, but then I discovered it's part of the C++ syntax for raw strings, not part of the regex. So I learned something new today!

9

u/AceOfShades_ Mar 10 '21

I am fascinated by the fact that even at `-O2` this spits out 28k+ lines of assembly when being compiled. Compilers are black magic.

5

u/HerrNilsen- Mar 10 '21

Not black magic, just the STL that is huge

5

u/RoLoLoLoLo Mar 10 '21

Why the extra backslashes in the raw string? Shouldn't R"([,!.?:;"/\\'[\]{}]+)" work just as well?

4

u/HerrNilsen- Mar 10 '21

Try yourself, I can't remember anymore

4

u/RoLoLoLoLo Mar 10 '21

Just did. Works for me.

4

u/HerrNilsen- Mar 10 '21

I was probably testing to much and overlooked that because.... regex and testing

2

u/Farewel_Welfare Mar 10 '21

Yeah raw strings exist so you don't have to escape them

5

u/The17ThCaillou Mar 10 '21

sorry but regex is not better than this code, regex is painfully slow

especially if you don’t know about the static keyword

2

u/_krinkled Mar 10 '21

Please explain further. Idk exact numbers but regex can be hella fast. What do you mean about the static keyword?

3

u/The17ThCaillou Mar 11 '21

regex is by no means fast fast to write when you grep? sure, fast in execution? definitely not the sole creation of the regex object is going to take 20 times longer than a ""naive"" parsing approach

this is why in c++ one would use the static keyword to prevent the object from being recreated each time

and even then, once the object done, I believe that most """naive""" approaches would still be faster than the regex counter part

1

u/_krinkled Mar 11 '21

What a load of bs. You must be new

1

u/The17ThCaillou Mar 14 '21

okay good for you

28

u/xVice1337 Mar 10 '21

How would you make it better?

53

u/[deleted] Mar 10 '21

This was from a project where we had to write a custom string class, so I would have probably written a custom string “removePunctuation()” function that uses a for loop to iterate through each character of a string, then using ispunct() I would check if each character is a punctuation character, and remove it if it was.

13

u/xVice1337 Mar 10 '21

Yea that's way cleaner, that's c++ right

32

u/[deleted] Mar 10 '21

Yep! I’m having a good laugh at this because this code is dogshit, but being able to recognize that and come up with a better solution shows that I actually learned something lol

3

u/xVice1337 Mar 10 '21

Experienced the same thing yesterday, wondered how tf I was so stupid back then

7

u/[deleted] Mar 10 '21

I would be worried about this solution, let's say you have the string:
".?A"

At first you look at the '.' at index 0, you're gonna remove it ofc.. but are you gonna move all the characters forward? Which would put the '?' in 0.. but you've already done le logic for index 0. If you change the length of the string, does your indexing system still make sense?

I worked on a problem like this recently and the correction suggested using streams to properly make changes such a string.

16

u/pitolaser Mar 10 '21

That's a good point! I would not move everything forward each time I delete a character. Lots of unnecessary copying. I would create a new string and append the old string characters only if they are not punctuation.

3

u/PecoraInPannaCotta Mar 10 '21

That's good but it can be better. In C++ creating a new string that is more than 20 chars results in a new allocation. This problem can be solved in o(n) without new strings.

3

u/Dadderdew Mar 10 '21

Or you can loop in reverse starting from the end of the string. That way you only modify data you already parsed and even if you remove a char and shift the tail of the string back to fill the gap you won't compromise anything that comes before

2

u/PecoraInPannaCotta Mar 10 '21

This still need more than a single shift if the punctuation is "fragmented" in the string

6

u/mattamatrow Mar 10 '21

Still better then regex

4

u/[deleted] Mar 10 '21

Shooting for one of those jobs that pays by the line

5

u/The17ThCaillou Mar 11 '21

the real r/badcode is people recommending regex

2

u/[deleted] Mar 11 '21

That is a hot take - don’t know enough about regex to argue but I would have just done like a SINGLE for loop instead of like 14 while loops and used the ispunct() function that freshman me apparently didn’t know existed

1

u/The17ThCaillou Mar 11 '21

this solution is indeed not the best but *thinking* that regex is the *best* alternative to this is not good

2

u/[deleted] Mar 11 '21

What else could I have done? This is a custom string class btw so any external methods you could normally do on a string I’d have to write myself and at that point it’s a debate on how much time I want to put into it

2

u/The17ThCaillou Mar 11 '21

string

remove_punctuation_reddit(string const& str)

{

string copy = str;

string const punctuation{ "/,!.?:;'\"[]{}" };

for (char const ch : punctuation) {

while (copy.contains(ch)) {

copy.remove(ch);

}

}

return copy;

}pretty much your approach but avoiding the copy pasting by using a loop

2

u/[deleted] Mar 11 '21

I never thought about it that way, thanks for the insight! I’m always looking to improve my coding

4

u/[deleted] Mar 10 '21

This hurts so much to look at lmao

3

u/[deleted] Mar 10 '21

How do you think I feel looking at it lol? Like I would have failed me just for that code segment alone

4

u/Promethean18 Mar 10 '21

Meanwhile the regexes have gone on strike!

10

u/[deleted] Mar 10 '21

I say kindness of Reddit but a lot of these comments are like “how the hell did you even get into this field” but like jokes on you buddy I heard that from my imposter syndrome before I heard it from the internet

7

u/[deleted] Mar 10 '21

The amount of regex comments I’m getting on this post is making me realize how much my programming languages prof has failed me, but thanks to the kindness of Reddit I’m getting a great crash course!

3

u/[deleted] Mar 10 '21

Does C++ support Regex? My first thought was to convert it to ascii then check to see if that fell in to that range then remove it, but wouldn’t Regex be easier?

2

u/[deleted] Mar 10 '21

From the comments, I think c++ does. And I’m learning that it would be a lot better than this dumpster fire I had earlier

3

u/myblindy Mar 10 '21

What code complexity do you want it to be?

Yes.

1

u/[deleted] Mar 10 '21

My time complexity is actually “fuck you”

5

u/IsSkyfalls_ Mar 10 '21

Why bother using string.contains anyway. Just call a bunch of string.remove or better, use a lookup table, iterate and call string.remove.

6

u/[deleted] Mar 10 '21

Tbh the contains method was custom written (as was my string class and other string methods). I didn’t have the knowledge/ability at the time to implement a lookup table 😅

3

u/tux_unit Mar 10 '21

Completely fair for a freshman, tbh

2

u/TheHairlessViking Mar 11 '21

C++ standard Regex is garbage in terms of both usability and performance

2

u/Sjuns Mar 10 '21

I mean it's still linear time in the length of the string, I could definitely imagine quadratic variants of this. Big O don't care about your repetitions. (You still should though.)

1

u/[deleted] Mar 10 '21

Either way this is some pretty sad code on my end lol - freshman me really needed to get her life together

2

u/potbread Mar 10 '21

I’d do it too just to avoid the horror of having to use regular expressions. Btw, is regular expression the ideal way to get this done or is there an alternate?

1

u/[deleted] Mar 10 '21

If I was rewriting this I would have just looped through the string once and removed punctuation whenever I came across it

2

u/Blackhaze84 Mar 10 '21

regex are for noobs

2

u/[deleted] Mar 10 '21

We write a 50 line code solution for a 4 line solution like real men

2

u/AliisAce Mar 10 '21 edited Mar 10 '21

Does c++ have an isalpha function?

If so:

for (char in string){

if (char !isalpha){

string = string.remove(char)}}

Might remove non Latin alphabet characters

1

u/[deleted] Mar 10 '21

It does have an isalpha function, but I would rather use ispunct to check for punctuation considering we needed to keep numbers in the strings and whitespaces as well

2

u/AliisAce Mar 10 '21

Fair enough. I haven't willingly touched c++ in a while

3

u/JYossari4n Mar 10 '21

This wouldn’t work because of iterator invalidation. Modern c++ solution would be remove by erase idiom.

2

u/AliisAce Mar 10 '21

I kinda wrote python

(Hides)

1

u/AutoModerator Mar 10 '21

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

/u/AliisAce, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/mr-mub Mar 10 '21

What is this font? (+_+)

2

u/[deleted] Mar 10 '21

I don’t even remember - I have it set to like comic sans now

2

u/Kyidou Mar 10 '21

Did they... did they never teach you if statements?

2

u/[deleted] Mar 10 '21

Oh no they did, freshman me probably just thought it was smart to have separate statements for each kind of punctuation rather than looping through the string once and checking for punctuation with ispunct()

2

u/Kyidou Mar 10 '21

lol did you not think of just wrapping the whole thing in a while loop?

1

u/[deleted] Mar 10 '21

I probably thought it would contain too many if statements in a single while loop (so why the hell did I do multiple while loops instead? Guess we’ll never know)

2

u/cur-o-double Mar 10 '21

So who is Fontenot?

2

u/[deleted] Mar 10 '21

My professor - I’m surprised he didn’t execute me for this code but I’ve definitely learned a lot from him

2

u/CreatureWarrior Mar 10 '21

When I started coding, I made a currency converter that had five currencies and 250 lines of code. Yeah, a lot of if/else statements there. Now I made it in 23 lines. And after a few years, I'll probably go "wtf was I thinking with that 2021 currency converter lol"

2

u/[deleted] Mar 10 '21

That’s a great sign though that you’ve learned. And I think that’s what’s important at the end of the day

1

u/CreatureWarrior Mar 10 '21

Yeah I agree :)

2

u/Pauchu_ Mar 10 '21

So... whos paying my eye surgery?

1

u/[deleted] Mar 10 '21

I’ll cover it, my bad bro

2

u/designxtek9 Mar 11 '21

I would probably have done the same in college. Stackoverflow didn’t exist back then.

1

u/[deleted] Mar 11 '21

Yoooo how did you even survive lol?

2

u/designxtek9 Mar 13 '21

Stackoverflow exists now lol. Saved.

2

u/[deleted] Mar 11 '21

Okay I'll preface this by saying I code in python and I only know that and html (which isn't a coding language ik but whatever).

Would it be correct that a better way to do this would be by creating a list of all the possible things you'd want to remove and then making a function that tests if an input has any punctuation and if so, deletes that punctuation?

2

u/[deleted] Mar 11 '21

Yes that’s a great solution! Too bad I didn’t even think of that when I was a freshman

2

u/[deleted] Mar 12 '21

Thank you! I'm a highschool junior rn and I think I've gotten the hang of python pretty well but now I'm just working on validating my knowledge and using more advanced features like OOP which is still kinda confusing for me

2

u/dimensionalsquirrel Mar 11 '21

I wonder if it took a long time to write that all out. I bet you could write a script to type it out for you, maybe even use a while loop to do it.

1

u/[deleted] Mar 11 '21

I bet I just copied and pasted the first while loop and changed the punctuation in each

2

u/ijp1016 Mar 11 '21

Come on guys, atleast there is a comment above the function definition. Lets all take a moment to appreciate that

1

u/[deleted] Mar 11 '21

Not even a professional comment 😭 just a “my professor told me to remove this”

131

u/Ali3nat0r Mar 10 '21

This runs in O(fuck) time

11

u/indentuum Mar 10 '21

O(n2)

21

u/HonzaS97 Mar 10 '21

Nah, it's still O(n), the multiplication constant is 14 though.

21

u/indentuum Mar 10 '21

I think that remove method removes only one character. That can explain why there is while instead of if.

4

u/HonzaS97 Mar 10 '21

Oh yeah, that's true actually.

2

u/[deleted] Mar 11 '21

I think it depends on the performance of the remove character function. If it is constant then it runs in O(n) otherwise O(n2) (could be worse but unlikely)

2

u/Startresse Mar 11 '21

depends on how the contains function is implemented. if it checks from the beginning of the string everytime it's O(n2 ).

86

u/[deleted] Mar 10 '21 edited Mar 10 '21

re.sub(r'[\W_]+', '', <string>) is the best i can come up with.

22

u/[deleted] Mar 10 '21

that would also remove whitespace

14

u/[deleted] Mar 10 '21

right, didn't think about that.

18

u/baubleglue Mar 10 '21

... and it is Python not C++ :)

Not clear why "Indexer" removes punctuation

https://github.com/muralidharm/DataStructuresFinalProject/blob/master/DSString.cpp#L240

I don't remember C++ well, probably something string.remPun(); should work if DSString done right.

32

u/[deleted] Mar 10 '21

Better than: for each char in string if char is not in abcdefghijklmnopqrstuvwxyzABCEFGHIJKLMNOPQRSTUVWXYZ

0

u/[deleted] Mar 10 '21

[deleted]

48

u/[deleted] Mar 10 '21

Yeh that’s the point

118

u/Phoenix030_xd Mar 10 '21 edited Mar 10 '21

Image Transcription: Code


//removes the punctuation that Fontenot told us to remove

Void Indexer::removePunctuation(DSString& string)

{

while (string.contains(‘,’))

    string = string.remove (‘,’);

while (string.contains(‘!’))

    string = string.remove (‘!’);

while (string.contains(‘.’))

    string = string.remove (‘.’);

while (string.contains(‘?’))

    string = string.remove (‘?’);

while (string.contains(‘:’))

    string = string.remove (‘:’);

while (string.contains(‘;’))

    string = string.remove (‘;’);

while (string.contains(‘\’))

    string = string.remove (‘\’);

while (string.contains(‘”’))

    string = string.remove (‘”’);

while (string.contains(‘\\’))

    string = string.remove (‘\\’);

while (string.contains(‘/’))

    string = string.remove (‘/’);

while (string.contains(‘[’))

    string = string.remove (‘[’);

while (string.contains(‘]’))

    string = string.remove (‘]’);

while (string.contains(‘{’))

    string = string.remove (‘{’);

while (string.contains(‘}’))

    string = string.remove (‘}’);

}

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

75

u/HerrNilsen- Mar 10 '21

Good human, now take vaccations

1

u/Aphix Mar 11 '21

Ew what? Big Pharma in my /r/badcode? I guess that's kinda ironic.

30

u/Naeio_Galaxy Mar 10 '21

Seriously, I sometimes wonder where you guys find the time for all this transcription... it's well done, thanks !! ♥️

6

u/War-Whorese Mar 10 '21

Took you a while.

0

u/[deleted] Mar 11 '21

[deleted]

2

u/Naeio_Galaxy Mar 13 '21

Lol without you I wouldn't understand the joke

5

u/Johanno1 Mar 10 '21 edited Mar 11 '21

I bet there is a regex for your problem.

But in the time you have figured out how it works and made a oneliner that does the same you probably have written down this code three times.

6

u/ArtisticSnek Mar 10 '21

You can do that with a dictionary and a for loop

2

u/[deleted] Mar 10 '21

This is what happens when you get bonuses based on lines of code

2

u/OriginalSynthesis Mar 10 '21

I don't blame you. Regex can be hard when you start out.

2

u/ViperLordX Mar 10 '21

O(n^2) and then some

2

u/AnotherBrug Mar 11 '21

Here is my Rust solution:

fn remove_punctuation(string: &mut String) {
    string.retain(|c| !c.is_ascii_punctuation());
}

2

u/Zealousideal-Rip-630 Mar 13 '21

how think you could fix it:

make a table of disallowed chars and check for it that way?

im not the best at anything c so im talking out of my ass here

1

u/Garegin16 Jun 19 '21

If regex didn’t exist. This is basically reasonable.

0

u/[deleted] Mar 10 '21

[deleted]

199

u/[deleted] Mar 10 '21

Lmao come see me when you grade for an intro to c++ class (also I would give you an award if I had money)

2

u/dronz3r Mar 11 '21

Lol I remember when I was a teaching assistant for intro programming course in college. I've seen students hardcoding outputs and typing each line of c++ code on terminal and press enter for compiling.

3

u/[deleted] Mar 11 '21

Oh my gosh I hate when they hardcode outputs - like do they really think we’re going to miss that detail and give them full credit?

0

u/[deleted] Mar 10 '21

[deleted]

55

u/[deleted] Mar 10 '21

...At least I get paid for this lol

18

u/ConfigAlchemist Mar 10 '21

...but is it worth it?

29

u/[deleted] Mar 10 '21

All jokes aside, the grading part is tough sometimes, but being able to help a student solve a problem with their programming is very rewarding and it brings me joy to watch other people learn something about programming and enjoy it at the same time

8

u/ConfigAlchemist Mar 10 '21

I wanted to go into teaching for exactly this reason, but, from what I can tell, it is impossible to make a middle class income off a high school teacher’s salary.

0

u/[deleted] Mar 10 '21

[deleted]

40

u/Sulla87 Mar 10 '21 edited Mar 10 '21

C++ has plenty of useful abstractions for different use cases.

In this case you could use std::string::replace which can hardly be called low level programming.

Also Java is a quite high level programming language. It won't let you access memory directly, and it manages all memory allocation and de-allocation for you through it's garbage collector, no pointers allowed. It doesn't even run directly on the hardware but through translation via the Java VM... There is really nothing low level about Java.

8

u/BongarooBizkistico Mar 10 '21

I read their comment as "java isn't low level and it's hard, so I can only imagine c++"

0

u/[deleted] Mar 10 '21

[deleted]

10

u/Sulla87 Mar 10 '21

I know what they meant and I'm not trying to be mean or arrogant, I'm just sharing knowledge because i think it is important to know the difference. Just think of it as piece of information.

Java is one of my main languages and I'm one of those people who actually like using it, so I'm not trying to shit on Java either.

4

u/PepperCheck Mar 10 '21

Ah, yeah I probably should’ve inferred that. Thank you though for pointing stuff like this out, I agree that it’s important to acknowledge misconceptions.

0

u/[deleted] Mar 10 '21

[deleted]

10

u/autobtones Mar 10 '21

........ wut?

5

u/Naeio_Galaxy Mar 10 '21

Good luck with the pointers or the heap memory when you'll start C/C++, or the smart pointers with Rust ! 🙃

0

u/[deleted] Mar 10 '21

[deleted]

21

u/[deleted] Mar 10 '21

Please never feel embarrassed for asking! This post is supposed to be me reflecting on how far I’ve come personally, and a little dig at myself for letting this get into a final assignment.

So let me activate c++ TA mode real quick. There’s this library called the ctype library (let me find a link, I’ll edit this post with documentation). The ctype library has many functions, one of which is a function called ispunct(char) which checks if a character is a punctuation character or not. Using this, one could iterate through a string using a for loop instead, and check each character of the string using ispunct(string[iterator]). If a particular character is a punctuation character, then you could remove it from the string. The reason why this implementation is pretty bad, is that essentially I think it’s taking up like 28 (?) lines and using 14 while loops to continuously check for specific punctuation characters throughout the whole string multiple times instead of going through the string once and taking out the punctuation character by character.

Edit: ctype documentation: http://www.cplusplus.com/reference/cctype/

Let me know if that made sense, it’s very late at night/earlier in the morning so I might not be cohesive

10

u/blerp_2305 Mar 10 '21

You could use regex which would be much simpler. Or you can loop through the string, check if that char is a punctuation and then remove it.

0

u/[deleted] Mar 10 '21

[deleted]

12

u/Sjuns Mar 10 '21

If you do that you should loop through the string once, with one for loop, and then for each character check and possibly remove it. The contains method implicitly loops over the whole thing and is called way too often. Could be kinda okay if you don't care that much about small performance differences but at least the whiles should be ifs, since they never reach the next iteration anyways.

0

u/[deleted] Mar 10 '21

[deleted]

3

u/[deleted] Mar 10 '21

newString = s for s in string if s not in punc

Would be the more pythonic way