Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Soricidus
Oct 21, 2010
freedom-hating statist shill

Pavlov posted:

Even with a US keyboard, I would use a language that uses 「Japanese quotation marks」, just because I think they look spiffy.

e: Holy poo poo they got 『』, 《》, and【】 too. Think of all the special nesting types we could have.

sempython

Adbot
ADBOT LOVES YOU

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
Most of the time when I look at my old code unhappiness stems from realizing my old assumptions in designing the codebase were flawed, incomplete or no longer valid. Sometimes you need a first stab at a problem and some time to see these kinds of design mistakes.

For things that are naturally self-contained I'm more likely to look back and see a mixture of opportunities to improve and pleasantly clever bits.

Soricidus
Oct 21, 2010
freedom-hating statist shill

Xerophyte posted:

Oh yes, the layouts themselves are horrors in a lot of ways. I maintain that the person who looked at the $ character and thought "that'd make a good special character in my spiffy programming language" was a bit blinkered, though.

It's not used in any good language I can think of, unless you count regex strings or non-user-facing stuff like name mangling.

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe

Soricidus posted:

It's not used in any good language I can think of, unless you count regex strings or non-user-facing stuff like name mangling.

I was going to counteroffer Perl and PHP, but you had to go and add that "good" modifier there.

(Disclaimer: I like Perl, but I would not want to work in a Perl shop)

raminasi
Jan 25, 2005

a last drink with no ice

Soricidus posted:

It's not used in any good language I can think of, unless you count regex strings or non-user-facing stuff like name mangling.

String interpolation in C# 6.

fritz
Jul 26, 2003

Soricidus posted:

You could try trigraphs.

Mega-rude.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

raminasi posted:

String interpolation in C# 6.

Also anonymous closure arguments in Swift. They're really nice when the closure is tiny.

b0lt
Apr 29, 2005

M31 posted:

Or C!

code:
main(int argc, char *argv<::>) <%
    printf("Hello World");
%>

Trigraphs are going to be removed in C++17, with IBM being dragged kicking and screaming about their "totally real but we can't talk about them" EBCDIC-using customers. Hopefully C will follow?

sarehu
Apr 20, 2007

(call/cc call/cc)
https://blog.devmastery.com/how-to-win-the-coding-interview-71ae7102d685

quote:

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. Allowances may be made for adjustments to capital letters, punctuation, and word dividers. Examples in English include “A man, a plan, a canal, Panama!”, “Amor, Roma”, “race car”, “stack cats”, “step on no pets”, “taco cat”, “put it up”, “Was it a car or a cat I saw?” and “No ‘x’ in Nixon”.

Write the most efficient function you can that determines whether a given string is a palindrome.

Okay...

quote:

On this particular challenge, I am expecting many will use RegEx as a part of the solution.

:psyduck:

Edit:

quote:

Before I start though, I have to say, if a company is going to hire a developer based solely and entirely on a piece of code the developer wrote in an interview, you probably don’t want to work there.

Ah, but of course we cannot forget culture fit.

sarehu fucked around with this message at 01:52 on Jun 1, 2016

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Unsurprisingly his example of a good solution is kinda poo poo.

Pollyzoid
Nov 2, 2010

GRUUAGH you say?

b0lt posted:

Trigraphs are going to be removed in C++17, with IBM being dragged kicking and screaming about their "totally real but we can't talk about them" EBCDIC-using customers. Hopefully C will follow?

Those are digraphs though, and they're not going to be removed for a while.

Absurd Alhazred
Mar 27, 2010

by Athanatos

Xerophyte posted:

On that note, it remains slightly annoying to me how English and US-centric a lot of the character conventions in programming are. ${}[]| and co aren't particularly easy to type on a good half of the world's keyboards, yet languages keep using them all over the drat place. Backticks are by far the worst in the swedish/nordic/german layouts, since ` is a chord resulting in a dead key meaning half the time you end up writing á instead of `a.

You might reply that I should use a US layout. I do, which has the cost that if I want to write something in my native language then that's a bunch of additional effort instead. The entire situation could be avoided by sticking to basic text-oriented symbols available everywhere but some jackass just had to give distinct meanings to all their special keys. I nurture a small, vindictive hope to some day introduce the ¨ and € operators in a major language to ensure that person will be as mildly annoyed as I have been.

My other language is RTL and an entirely different alphabet. I have to use "alt-shift" to change languages multiple times a minute, when I'm talking to friends while looking up programming-related stuff. I manage, and so can you.

If you want things to be different, go back in time and make it so that the Swedes had invented and popularized programming or whatever. See if you can fend off the Israelis, Iranians, and Chinese who are trying to mess around with the same timeline, though. :shrug:

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."

Plorkyeran posted:

Unsurprisingly his example of a good solution is kinda poo poo.

Is this where we all trip over each other to make a better implementation and do point-by-point teardowns of solutions we don't like?

JavaScript code:
function isPalindrome(s) {
	var f = s.toLowerCase().replace(/[^a-z\d]/g, "");
	return f == f.split("").reverse().join("");
}
I think his insistence on handling bogus types with an explicit exception throw is silly; a non-string in this implementation will immediately explode for lack of a toLowerCase(). I also don't see any reason to complain about strings which are vacuously palindromes. Regular expressions are unnecessary for this type of problem, but I think they're reasonably defensible in light of the requirement to ignore characters beyond alphanumerics. I was surprised to discover that evidently JS regexes don't have a symbol equivalent to a-zA-Z0-9. This would also be simpler if JS Strings were genuinely Arrays.

senrath
Nov 4, 2009

Look Professor, a destruct switch!


Internet Janitor posted:

I was surprised to discover that evidently JS regexes don't have a symbol equivalent to a-zA-Z0-9. This would also be simpler if JS Strings were genuinely Arrays.

Isn't that \w?

Edit: Double checking apparently that also includes underscores. Huh.

FamDav
Mar 29, 2008
They didn't even parallelize the palindrome check

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
I have no idea why, but I have consistently seen multiple cases where people where people try to check if something is a palindrome by doing:

JavaScript code:
var leftHalf = string.slice(0, string.length / 2);
var rightHalf = string.slice(string.length / 2);
return leftHalf == rightHalf.reverse();
I really don't know what it is.

brap
Aug 23, 2004

Grimey Drawer
Writing a function that logs to the console when doing something very simple with no dependencies is just retarded.
That guy is definitely high on the sense that he's a PROFESSIONAL JavaScript developer and from sniffing his own farts.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

Suspicious Dish posted:

I have no idea why, but I have consistently seen multiple cases where people where people try to check if something is a palindrome by doing:

JavaScript code:
var leftHalf = string.slice(0, string.length / 2);
var rightHalf = string.slice(string.length / 2);
return leftHalf == rightHalf.reverse();
I really don't know what it is.

Because they can see that the "obvious" solution of checking whether string == reverse(string) ends up checking each letter twice, and they're too clever for their own good.

canis minor
May 4, 2011

quote:

“Do I need to handle unicode characters?”

Obviously the answer is no

isPalindrome("aaą");
> true

canis minor fucked around with this message at 07:39 on Jun 1, 2016

Soricidus
Oct 21, 2010
freedom-hating statist shill

Absurd Alhazred posted:

My other language is RTL

oh god trigger warning that poo poo dude

I literally still have nightmares about bidi text handling, over a year after I last worked on anything that involves it

is the thing where numbers are always LTR actually good from your perspective? because it's the reason there is never a nice simple case

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

I was going to say that he says that about regex, but then doesn't actually use regex as part of his model solution. But technically, he does - he uses it to filter out non-letter characters before he gets to the actual work of determining if it's a palindrome. I guess that's all he meant by "use regex as part of the solution". It's poor communication on his part, because he's unnecessarily drawing attention to a relatively unimportant preparatory step, which misleads the reader into thinking that the use of regex will be the meat of the solution. All round a fairly dubious article.

Jewel
May 2, 2009

I don't know if this is the right one because I don't really want to think about it too hard, but I have seen people check for palindromes with regex recursion. This is a perl regex (only works in certain versions or something?)

code:
/^((.)(?1)\2|.?)$/

TheresaJayne
Jul 1, 2011
Heres a new one,

A project decided to use bitmasks to store permissions in an int64.

They now have 65 permissions needed....

feedmegin
Jul 30, 2008

TheresaJayne posted:

Heres a new one,

A project decided to use bitmasks to store permissions in an int64.

They now have 65 permissions needed....

Hope their compiler supports uint128_t!

TheresaJayne
Jul 1, 2011

feedmegin posted:

Hope their compiler supports uint128_t!

VB.net

The MUMPSorceress
Jan 6, 2012


^SHTPSTS

Gary’s Answer
Aren't palindromes like the classic example of the recursive version being simpler to read and easier to conceive. How is any regex, at all, better and more comprehensible than:

code:
bool isPal(int leftIndex, int rightIndex, String string){
    if leftIndex==rightIndex return true;
    if string.charAt(leftIndex)!=string.charAt(rightIndex) return false;
    return isPal(leftIndex++, rightIndex++, string);
}

void main{
   String str="taco cat";
   System.out.println((Bool)isPal(0,str.Length(),str).toString());
}

I'm sure there's some fucky syntax in there, but it's been ages since I got to use a real language.

The iterative version isn't that much harder to implement (even using that while(var++ && var--) syntax argued about earlier), and either is way better than going straight to regular expressions imo.

edit:
code:
ispalindrome(string) n begin,end,fail
	s string=$$stripspaces(string)
	s end=$l(string),begin=1
	f  d  q:fail  q:end=begin
	. i $e(string,begin)'=$e(string,end) s fail=1 q
	. s end=end-1,begin=begin+1
	q 'fail

The MUMPSorceress fucked around with this message at 16:14 on Jun 1, 2016

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe
The regex was used because part of the problem statement was to ignore non-letters (and casing). I admit I have trouble thinking of a better way than a regex to strip out undesirable characters, unless the language being used specifically has a function that happens to meet my needs.

The MUMPSorceress
Jan 6, 2012


^SHTPSTS

Gary’s Answer

TooMuchAbstraction posted:

The regex was used because part of the problem statement was to ignore non-letters (and casing). I admit I have trouble thinking of a better way than a regex to strip out undesirable characters, unless the language being used specifically has a function that happens to meet my needs.

i can't think of any languages that don't have functions for moving to all caps, no caps, and stripping whitespace. stripping punctuation is a wrinkle i hadn't thought of for sure.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

LeftistMuslimObama posted:

Aren't palindromes like the classic example of the recursive version being simpler to read and easier to conceive. How is any regex, at all, better and more comprehensible than:

code:
bool isPal(int leftIndex, int rightIndex, String string){
    if leftIndex==rightIndex return true;
    if string.charAt(leftIndex)!=string.charAt(rightIndex) return false;
    return isPal(leftIndex++, rightIndex++, string);
}

Well, for one, it would terminate.

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

LeftistMuslimObama posted:

i can't think of any languages that don't have functions for moving to all caps, no caps, and stripping whitespace. stripping punctuation is a wrinkle i hadn't thought of for sure.

If you also take into account unicode, it suddenly becomes a big pain in the rear end, too. Like combining characters:

Les Mis\u030erables = Les Misérables

Reverse it!

selbare\u030siM seL = selbareśiM seL

Oops, the accent is over the wrong character now.

(Example stolen from Jon Skeet: https://codeblog.jonskeet.uk/2009/11/02/omg-ponies-aka-humanity-epic-fail/)

OddObserver
Apr 3, 2009

TooMuchAbstraction posted:

The regex was used because part of the problem statement was to ignore non-letters (and casing). I admit I have trouble thinking of a better way than a regex to strip out undesirable characters, unless the language being used specifically has a function that happens to meet my needs.

An if statement? Does everything have to be a precanned library function?

The MUMPSorceress
Jan 6, 2012


^SHTPSTS

Gary’s Answer

rjmccall posted:

Well, for one, it would terminate.

Lol, I always gently caress that up.

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe

OddObserver posted:

An if statement? Does everything have to be a precanned library function?

No, I was saying that in the absence of some kind of stripNonLetters() library method, a regex would be the only other good option for removing non-letter characters from a string that I can think of. And in fact I wouldn't be surprised if such a library method were just a thin wrapper around a regex anyway.

I mean, sure, you can iterate over every character and test if the char value of the character falls between 65 and 90 or 97 and 122 (or look up if the character is in a whitelist by other methods), assuming you're happy assuming ASCII. I don't think that's a superior solution though.

qntm
Jun 17, 2009
Unicode has no notion of string reversal, so I think it's fair to treat the input as an opaque sequence of code points or whatever.

OddObserver
Apr 3, 2009
Who said you need to mutate the string at all? (Well, C-form normalization probably is a good idea, yes).

Edit: what I mean is, whole thing is easily doable in a manner similar to the "collapsing walls" quick sort partition function, just w/o swaps.

OddObserver fucked around with this message at 17:05 on Jun 1, 2016

eth0.n
Jun 1, 2012
Thought I'd try doing it the "proper" C++ way, using iterators, and avoid string copying and redundant comparisons. Reverse iterators turn out to be kind of a pain. Would have been nice if they could directly be compared to forward iterators, or if there were a consistent way to "toggle" the reverse-ness of an iterator.

code:
template<typename Iter>
Iter skip_nonletter(Iter i, Iter limit)
{
  while(i <= limit)
  {
    char c = tolower(*i);
    if(c >= 'a' && c <= 'z')
      break;
    ++i;
  }
  return i;
}

bool palindrome(const std::string &s)
{
  auto left = s.cbegin();
  auto right = s.crbegin();
  for(;;)
  {
    left = skip_nonletter(left, right.base());
    right = skip_nonletter(right, std::reverse_iterator<decltype(left)>(left));
    if(left > right.base())
      break;
    if(tolower(*left) != tolower(*right))
      return false;
    ++left;
    ++right;
  }
  return true;
}
This isn't something I'd be able to cough up in an interview, though. Probably would just go C-style, with indexes, and somewhat sloppy near-duplication of the "skip nonletter" code (i.e., same, but reversed direction).

eth0.n fucked around with this message at 17:15 on Jun 1, 2016

xzzy
Mar 5, 2009

I can count the number of times I've had to reverse a string in the past 20 years on zero fingers.

Teachers should probably do a better job coming up with assignments that actually have application in the real world. Want to teach regex? Make them parse logs for specific data. Want to teach recursion? Implement quicksort or something.

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.
Using a regex is still bad because you need to at minimum perform a bunch of writes and likely an allocation. You have a fixed number of valid characters to test against, so you can just skip everything that isn't valid in constant time per character and run through with a forward and backward iterator in O(N) where N is the length of the input. Testing against both the character's upper/lower also takes constant time per test. As you need to at minimum read the whole string to check the characters, you're already at the best asymptotic time with reasonable use of memory and you can get on with your day and doing something relevant.

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe

leper khan posted:

Using a regex is still bad because you need to at minimum perform a bunch of writes and likely an allocation. You have a fixed number of valid characters to test against, so you can just skip everything that isn't valid in constant time per character and run through with a forward and backward iterator in O(N) where N is the length of the input. Testing against both the character's upper/lower also takes constant time per test. As you need to at minimum read the whole string to check the characters, you're already at the best asymptotic time with reasonable use of memory and you can get on with your day and doing something relevant.

I can write a regex to strip invalid characters a hell of a lot faster than I can write the relevant whitelist and loop to do it manually. Is the manual approach more efficient? Probably. But I'll be getting on with my day doing something relevant faster if I make full use of the available tools.

Adbot
ADBOT LOVES YOU

Absurd Alhazred
Mar 27, 2010

by Athanatos

Soricidus posted:

oh god trigger warning that poo poo dude

I literally still have nightmares about bidi text handling, over a year after I last worked on anything that involves it

is the thing where numbers are always LTR actually good from your perspective? because it's the reason there is never a nice simple case

In Hebrew numbers are spoken LTR/big-endian, so it works out well for typing. I have just realized, though, that people using physical typewriters must have been writing numbers RTL/little-endian.That must have been an irritation, both getting used to it, and then getting used to the new way.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply