[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Solve this, /g/

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 150
Thread images: 8

File: interview.jpg (162KB, 1000x750px) Image search: [Google]
interview.jpg
162KB, 1000x750px
>At interview
>Get this problem to solve

"Transform the number written in words into an integer number, using only 3 if statements"
Ex: One thousand thirty three = 1033

The "and" keyword is omitted

>Solve it with some hints, whiteboard programming is more difficult than it seems

How fast can you solve this, /g/?
>>
>>56386994
I walk out of the interview because they gave me a whiteboard coding question.
>>
>>56387000
How else would you ask the applicant to show their skill if you were an interviewer?
>>
>>56387010
Trial period working in the office e.g. a week
>>
>>56387000
This is the only correct answer.
>>
>>56387014
That's a lot of logistic work. Their don't have time for that shit.
>>
>>56387030
It's not like they'd set it up the day the interviewee comes in. For quality positions it's definitely worth the time.
>>
KEK
Anons cannot solve it, so they write all the shit above.
That's why you are all going to remain the NEETS you are
>>
File: 1469750251521.jpg (55KB, 500x535px) Image search: [Google]
1469750251521.jpg
55KB, 500x535px
>>56386994
Switch Case :^)
>>
>>56387040
But they'd still have to decide which candidates to offer the trial period to. There'd still be need for testing practical coding in the interview itself.

And for some positions time is of the essence, they simply don't have time to mess around with trials, they need to hire someone to start on an actual project ASAP.
>>
>>56387064
The applicant provides evidence of related experience.

>they need to hire someone to start on an actual project ASAP
That's what contractors are for.
>>
>>56386994
>"Transform the number written in words into an integer number, using only 3 if statements"
Not possible without very clever tricks
>>
>>56387089
No tricks, I assure you.
Hint: use a hash table
>>
>>56386994
what if i write one million and forty two? It will need more than three if. Also can i for?
>>
>>56386994
This question is stupid.
"Using only 3 if statements" is that a maximum of 3 if statements, or should my could just be 3 if statements?
If the latter, what restrictions are there on the blocks in the then/else branch?
If the former, can I use haskell?
>>
>>56387109
I think you can do this with just one in Python.
>>
>>56387116
If you can do it in less than 3 if statements, than you are better than the interviewer.
>>
>>56387125
>Python
i thought it was a serious question, sorry
>>

for (; <if statement> ;) {

// code

break;
}
>>
>>56386994
Int32.TryCast("1033");
just c# things
>>
>>56387158
OP has been asked the opposite, you imbecile.
>>
>>56386994
I turn 360° degrees and walk away because I am a Java enterprise fullstack developer with an expertise in JPA, (Oracle-, My, HSQLDB)-SQL, Spring, maven, jenkins, git, vue/angular/react SPAs and apparently mistakenly applied for a computer scientist job instead of a software engineer job.
>>
>>56387164
Even for a CS job whiteboard programming is outright insulting.
>>
digits = ['zero', 'one', 'two', ... ,'nine'];
teens = ['ten', 'eleven', 'twelve', ... 'nineteen']
tens = ['twenty', 'thirty', 'forty'... 'ninety'];
powers = ['thousand', 'million', 'billion', ... ];


function question(string) {
result = 0;
nextword = string.nextToken();
while (nextword) {


if (nextword in tens) {
result += (tens.indexOf(nextword)+2)*10;
(nextword = string.nextToken() && result += digits.indexOf(nextword));
return;
}

if (nextword in digits)
mult = digits.indexOf(nextword);

if ((nextword = string.nextToken()) {
result += mult * exp(10, 3 * (powers.indexOf(nextword)+1));
}

}
>>
>>56387162
This would be the opposite

((Int)1033).ToString()
imbeshchill
>>
>>56386994
Does that mean that you must solve it by using only 3 functions which all must be if()-s, or are you allowed to use any number of functions and methods where the maximum number of allowed if()-s are 3?

(Does asking such question at an interview make you immediately fail or generally considered as a bad point?)
>>
>>56386994
if (a[0] == 'O' &&
a[1] == 'n' &&
a[2] == 'e' &&
a[3] == ' ' &&
...

So efficient I only need one if statement, I'll take my job now cuckboy
>>
>>56386994
It would probably take me a while. There are a few edge cases to consider.
>>
Java BigInteger :^)
>>
>>56387182
Good luck with the number "one thousand two hundreds".
>>
>>56387204
oh fuck I forgot about hundreds
>>
values = {
'one' => 1,
'two' => 2,
'thirty' => 30,
...
}

mods = {
'hundred' => 100,
'thousand' => 1000,
...
}

arr = strsplit(' ', input)
reverse(arr)

sum = 0
mod = 1
for(word in arr){
if mods[word]
mod = mods[word]
else
sum += mod*values[word]
}

didn't give it much thought
>>
Replace stuff:
nineteen -> one ten nine
twenty -> two ten
>>
>>56387235
i like the elegance and simplicity of anaconda
good job anon
>>
Dictionary of every word to number (0-9)
Dictionary of how many zeroes one, one hundred, one thousand etc contains

read until you reach a hundred, thousand or a million etc
create an array or something like that with the amount of total digits you need based on what you read using the dictionary of zero counts
read the next word
insert it into the array
if the next word isn't a number, rather a thousand, hundred etc skip the zeroes
else insert the number
repeat

Give me a job
>>
I would probably solve it with Regex, and recursion.
>>
>>56387281
You can't parse numbers with regex. Because numbers can't be parsed by regex. Regex is not a tool that can be used to correctly parse numbers. As I have answered in numbers-and-regex questions here so many times before, the use of regex will not allow you to consume numbers. Regular expressions are a tool that is insufficiently sophisticated to understand the constructs employed by numbers. numbers is not a regular language and hence cannot be parsed by regular expressions. Regex queries are not equipped to break down numbers into its meaningful parts. so many times but it is not getting to me. Even enhanced irregular regular expressions as used by Perl are not up to the task of parsing numbers. You will never make me crack. numbers is a language of sufficient complexity that it cannot be parsed by regular expressions. Even Jon Skeet cannot parse numbers using regular expressions. Every time you attempt to parse numbers with regular expressions, the unholy child weeps the blood of virgins, and Russian hackers pwn your webapp. Parsing numbers with regex summons tainted souls into the realm of the living. numbers and regex go together like love, marriage, and ritual infanticide. The <center> cannot hold it is too late. The force of regex and numbers together in the same conceptual space will destroy your mind like so much watery putty.
>>
>>56387281
If you parse numbers with regex you are giving in to Them and their blasphemous ways which doom us all to inhuman toil for the One whose Name cannot be expressed in the Basic Multilingual Plane, he comes. numbers-plus-regexp will liquify the nerves of the sentient whilst you observe, your psyche withering in the onslaught of horror. Rege̿̔̉x-based numbers parsers are the cancer that is killing /g/ it is too late it is too late we cannot be saved the trangession of a chi͡ld ensures regex will consume all living tissue (except for numbers which it cannot, as previously prophesied) dear lord help us how can anyone survive this scourge using regex to parse numbers has doomed humanity to an eternity of dread torture and security holes using regex as a tool to process numbers establishes a breach between this world and the dread realm of c͒ͪo͛ͫrrupt entities (like SGML entities, but more corrupt) a mere glimpse of the world of regex parsers for numbers will instantly transport a programmer's consciousness into a world of ceaseless screaming, he comes, the pestilent slithy regex-infection will devour your numbers parser, application and existence for all time like Visual Basic only worse he comes he comes do not fight he com̡e̶s, ̕h̵is un̨ho͞ly radiańcé destro҉ying all enli̍̈́̂̈́ghtenment, numbers tags lea͠ki̧n͘g fr̶ǫm ̡yo͟ur eye͢s̸ ̛l̕ik͏e liquid pain, the song of re̸gular expression parsing will extinguish the voices of mortal man from the sphere I can see it can you see ̲͚̖͔̙î̩́t̲͎̩̱͔́̋̀ it is beautiful the final snuffing of the lies of Man ALL IS LOŚ͖̩͇̗̪̏̈́T ALL IS LOST the pon̷y he comes he c̶̮omes he comes the ichor permeates all MY FACE MY FACE ᵒh god no NO NOO̼OO NΘ stop the an*̶͑̾̾̅ͫ͏̙̤g͇̫͛͆̾ͫ̑͆l͖͉̗̩̳̟̍ͫͨe̠̅s ͎a̧͈͖r̽̾̈́͒͑e not rè̑ͧ̌aͨl̘̝̙̃ͤ͂̾̆ ZA̡͊͠͝LGΌ ISͮ̂҉̯͈͕̹̘̱ TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ
>>
>>56387344
>>56387349
Is this copypasta?
>>
>>56387367
It's about parsing HTML with regex.
>>
>>56387052
/thread
>>
>>56387367
https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags
>>
>>56387235
I like this one
>>
String number = "1031";
Int number_int = Integer.parseInt(number);
._.
>>
>>56387000
>>56387167
Why is this such a bad thing? Seriously, is this considered an insulting task, and why? I'm not a programmer, so I don't really understand.

Is it because they're trying to put you though a kind of childish high-school/college tier exam/pop quiz type thing?
>>
>>56387349
How do you write like this?
sauce?

H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ
>>
>>56387538
Are you fucking retarded, faggot?
Read OP's post again
>>
Impossible. Without bounds, it would require support for an infinite number of conversions in your lookup tables.
>>
>>56387607
How is 2^31 - 1 for a bound
>>
>>56387235
Solve for:
one thousand thirty fifty sixty one thousand
>>
>>56387000
This

Nothing pisses me off than stupid interviewers who think you're a bad programmer because you can't remember some shitty algorithm that only a bookworm college graduate would remember

YOU'RE ASKING THE WRONG QUESTIONS, MEMORIZING ALGORITHMS DOES NOT MAKE YOU A GOOD PROGRAMMER, ASK ME ABOUT ARCHITECTURE OR WHAT TECHNOLOGY TO USE TO SOLVE A SPECIFIC PROBLEM, OR HOW TO APPROACH A CERTAIN PROBLEM

But no, let's ask about some shitty language related syntax or sorting algorithm. I'm not a fucking encyclopedia, we have google for that, we are paid to think, design and solve, not memorize
>>
>>56387637
that's not a valid number though.
>>
>>56387650
Tell us what exactly do you see in this problem that is about memorizing. This is a thinking problem, which you obviously fail at.
It also seems like most of /g/ pisses its pants at the sight of a cute little problem like this one.
Also, >>56387235
is incorrect
>>
I would tell them right away that I would first Google it, because I could no way come up with the most efficient solution. If they forced me to do it, maps, but it would barely work and not for many cases. Dealing with two digit numbers s a pain in the ass.
>>
File: 20160902120232197.png (5KB, 1110x65px) Image search: [Google]
20160902120232197.png
5KB, 1110x65px
>>56386994
For something crudely thrown together in 20 minutes, it works okay.
>>
>>56386994

I did it, OP.

My code is not very beautiful, but it works..

simple_digit = {"one" => 1, "two" => 2, "three" => 3}
digit = {"eleven" => 11, "twelve" => 12, "twenty" => 20, "thirty" => 30}
multiply = {"million" => 1_000_000, "thousand" => 1_000, "hundred" => 100}

input = "one million two hundred thousand twenty three"
output = 0
flag_new_number = true
new_number = 0

input.split.each do |w|

if simple_digit.key? w
output += new_number
new_number = simple_digit[w]
else
new_number += digit[w] if digit.key? w
new_number *= multiply[w] if multiply.key? w
end
end
output += new_number

puts output
# 1200023



But it was more difficult than I thought..
>>
>>56388301

Obviously I didn't type in every possible digit, but you get how it works..
>>
>>56388301

whoops, I forgot to delete this line here:

>flag_new_number = true
>>
>>56388301
Yeah, that's it.
I solved it a bit differently.
A single dictionary
take the number
compare it in three if statements:
is it less than 100
is it == 100
is it bigger than 100(as in 1000, 10000 etc)
I also had a temp variable which I added to the final sum only in the third if statement or at the end
>>
>>56388399
Full disclosure:
This question was at Microsoft for an internship
They do tend to give easy problems which require good thinking on the spot. Oh, and they really care about the edge cases in their interviews. Even if you think you solved it, they almost always find a test case to fuck with your code. The people interviewing you are crazy smart, at least the Visual Studio team
Microsoft shilling: off
>>
>>56388399

Yup, your approach looks nicer:

muh_dic = {"one" => 1, "two" => 2, "three" => 3, 
"eleven" => 11, "twelve" => 12, "twenty" => 20, "thirty" => 30,
"million" => 1_000_000, "thousand" => 1_000, "hundred" => 100}

input = "one million two hundred thousand twenty three"
output = tmp = 0

input.split.each do |w|
if muh_dic[w] < 10
output += tmp
tmp = muh_dic[w]
elsif muh_dic[w] < 100
tmp += muh_dic[w]
else
tmp *= muh_dic[w]
end
end
output += tmp

puts output
>>
>>56388440

Pretty tough..

I guess they don't expect you to solve it perfectly.

But it took me a few trials to get that you have use a temp variable. And even though I was doing this for fun and not with huge motivation, I'm relaxed and comfy right now.

To solve this when you are under pressure and surveillance.. pretty intense.
>>
Here's a series of replacements. It is not code or pseudocode, but it should help you guys. It's verbose as fuck for didactic.

0. Example number - 26 304 516 260
>Twenty-six billions, three hundreds and four millions, five hundreds and sixteen thousands, two hundred and sixty

1. Remove the crap.
>twenty six billion three hundred four million five hundred sixteen thousand two hundred sixty

2. Replace nineteen=one ten nine, ninety=nine ten, etc.
>two ten six billion three hundred four million five hundred one ten six thousand two hundred six ten

3. Replace one=1, two=2, etc.
>2 ten 6 billion 3 hundred 4 million 5 hundred 1 ten 6 thousand 2 hundred 6 ten

4. Encircle.
>(2 ten 6 billion 3 hundred 4 million 5 hundred 1 ten 6 thousand 2 hundred 6 ten)

5. Replace thousand=)*1000+(, million=)*1000000+(, etc.
>(2 ten 6)*1000000000+(3 hundred 4)*1000000+(5 hundred 1 ten 6)*1000+(2 hundred 6 ten)

6. Replace ten=*10+ and hundred=*100+
>(2*10+6)*1000000000+(3*100+4)*1000000+(5*100+1*10+6)*1000+(2*100+6*10)

7. SOLVE THE FUCKING MATH.
>>
>>56388301
Pretty good.

I'm shit as fuck at programming so it's always interesting to see how people solve problems I would probably spend hours, if not days, to solve if I can't find an example or something similar online.
>>
>>56386994
2hard im no genius
>>
>>56388724
Actually, the list of replacements after the step 1 can be simpler (slash means replacement)
,[space] / [space]
[space]and[space] / [space]
s[space] / [space]
s[word end] / [word end]
- / [space]
[word start] / [word start](
[word end] / )[word end]
eleven / 11
twelve / 12
teen / +10
ty / *10+
one / 1
two / 2
three / 3
thir / 3
[...]
nine / 9
hundred / *100+
thousand / )*1000+(
[...]
+) / )
+() / [nothing]

The last two rules are for cases like a number ending in a ty and a power of 1000 respectively.

This should generate a simple equation from any English number name, with no exceptions.
>>
>>56388724

From the way OP formulated the problem I'd say we can expect well-defined input.

This "replacements" of yours doesn't really make the problem more difficult or a challenge, it's just more annoying becasue we have to sanitize the input first. It's literally just the reverse operation of what you did, just a littel bit of fucking arround with the input.

In this case I'd just throw a few replacements at it. And then check if there's any element that's not know - if so, it's not a well-defined input and we throw an exception at the impolite user.


>>56388743

>I'm shit as fuck at programming

We are all on our way to get better, anon.
>>
>>56387561
I'm genuinely curious too.
>>
>>56386994
You need neural language networks for that.
>>
>>56387000
did you do a 360 before walking away?
>>
>>56386994
I can do it with only 1.
if words == "One thousand thirty three":
return 1033
>>
>3 if statements
>implying all you fags using dicts and hashmaps even understand that your implementations does way more than 3 conditional branches

You failed the moment you declared your arrays.
>>
>>56389444
Trips'o truth m80
>>
>>56389236
I'm actually more concerned about doing it in a bash-friendly way to avoid proper programming. Yes, I'm that lazy.
>>
>>56389541

Autists will not grasp that words are meaningless without context.

If somebody says "write a programm with three if -statements" he doesn't mean "write a programm that has a bytcode with three if statements". It's about using the if-statement (including stuff like elseif, switch case..) in your code only three times.

Those criteria are only there to point you in the right direction, because they DON'T want a solution that goes like "if text = 'one hundred twenty' then.."


>>56387561
>>56389339

Sigh, it all depends..

Whitboard coding is a pain in the arse because it's something you don't usually do. It's like saying you want to make a job interview for musicians but they have to play their instrument with the left hand.

However, if the interviewers are cool and are more interested in your general problem solving skills (instead of nitpicking about tiny syntax details) it's all fine.

I guess most people just don't like the pressure you feel when doing coing on a whiteboard. Not that I think whiteboard coding is such a godd idea, but it's also not that terrible if you are prepared.
>>
>>56389703
OP hasn't specified a langage, so I'm assuming there's a solution for every langage.

>he doesn't mean "write a programm that has a bytcode with three if statements".

How would you do it in C, then?
>>
>>56389733
If we're going down this path I'm pretty sure there's a language down here on the internet with a built-in statement to do just that :/
>>
>>56389667

OK, my bad.

You want to solve it by gradually changing the input..

Don't know if this will work out, but interesting thought.
>>
>>56389774
That's the idea. And I've done it as far as

./number <<< 'Twenty-six billions, three hundreds and four millions, five hundreds and sixteen thousands, two hundred and sixty'


Yielding
(2*10+6)*1000000000+(3*100+4)*1000000+(5*100+6+10)*1000+(2*100+6*10)


Now I just need to discover how to read said expression in bash and output the result.
>>
File: 1373175122247.jpg (167KB, 445x565px) Image search: [Google]
1373175122247.jpg
167KB, 445x565px
>>56387000
Whiteboard coding isn't terribly reflective of actual programming (this is also what screwed me up in pencil-and-paper programming exams), but having been in many engineering interviews whiteboards are better than the alternative.

>go to interview
>"tell me about yourself"
>"tell me about a time where you..."
>"why are you interested in our company?
>get rejected for "not being a fit" or some shit

Human resources was a mistake.
>>
>>56389801
Isn't bash capable of basic math?
>>
>>56389703
>Sigh,
>>
>>56389802
Fucking pen and paper logic
I hate fucking having to write my logic painfully, line by line, only to be sworn at when "It doesn't work that way you can't even do basic programming anon"

Tell that to the 22 Fuckers out of 36 who completely failed the exam
>>
>>56386994
>using only 3 if statements
how about no
>>
>>56389802

You are probably one of those autists that have no clue about the company where they are applying ("well, google made this search engine, right?") and don't even understand what people want to hear when they ask you something.

Let me guess: you also don't have a girlfriend because "wow, nice boobs" somehow never hits home, right?
>>
>>56389902
The problem is that even experienced programmers make silly errors the first time through, and correct them after the compiler or interpreter yells at them.

I wasn't even taking a real programming class, I was just taking the mandatory Excel (seriously) and MATLAB freshman courses (the whole freshman general engineering program at my school is dumb, at best you learn basic SolidWorks).
>>
>>56387052
for loop or while loop can be used for this too
>>
>>56390001
I remember when I was around 16 we had "Excel classes".
We had to write excel functions on paper, the longest one I can remember was eight fucking functions long.
>>
>>56386994

well you would have to store all the words at some point, so why not just have a 10 element array for each digit filled with strings, iterate through each digit starting at the least significant, append the element of each digit to a string and return the string
>>
>>56387158
is everyone that uses c# that stupid or just you?
the OP asked to convert "One thousand thirty three" to int, not "1033"
>>
>>56389959
>and don't even understand what people want to hear when they ask you something

That's the thing, of course I get that I'm supposed to give some flattering bullshit, I just don't know their preferred flavor.

There was one company where I actually did pretty well and came close, but from what I know they halted hiring to redirect resources to impressing GE (they were still on venture capital).
>>
>>56390148

You see, an interview is pretty similar to flirting with someone at the disco:

They expect you to tell lies and try to find your weak spot. You have to be:
-cool (friendly, nice, confident, professional) as fuck
-you need to show social skills (you can talk smoothly, be funny, you know the company, you know your strengths and weaknesses)
-you have the "je ne sais quois" (maybe you aren't talkative but know your devops inside out? maybe you aren't the best coder ever but a real work horse? maybe you are lazy but brilliant in some field? maybe you are pretty average but have a lot of passion?)
>>
>>56390250
I'm able to consistently get laughs from interviewers if nothing else. I recall one evaluation said I had strong self-confidence and appearance, so it's reasonable to say I'm not fucking up my suit. They did say I have weak communication, though, so yeah I probably am still a fucking autist.

I know almost for a fact that I'm much worse at personality-gauging questions than anything job-related. There's also the possibility everyone else has engineering-related connections (my family is liberal arts professors) or I'm basically competing with a bunch of guys who fix cars for fun (hell that's pretty much what cost me a job with a power tool company).
>>
>yet another "do my homework" thread /g/ falls for
Sigh. Please just let people that can't do simple goddamn homework assignments fail their goddamn classes so they can just go ahead and fall back into some retarded easymode major like business or drop out and work at a McJob the rest of their lives.
>>
>>56390398

Go back to your desktop/speccy thread and let the big guys code, will you?
>>
>>56390398
Well aren't you the smart one.
kys spergm nobody likes you
>>
>>56386994
Why don't you have lists with the strings "one" "two" "three" and so on and then index them
>>
>>56389864
Yes, but I was struggling with the syntax. This now works:

#!/bin/bash
e=$(sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
s/, / /g
s/ and//g
s/s / /g
s/s$//g
s/-/ /g
s/^/(/g
s/$/)/g
s/teen/+10/g
s/ty/\*10+/g
s/one/1/g
s/two/2/g
s/twen/2/g
s/three/3/g
s/thir/3/g
s/four/4/g
s/five/5/g
s/six/6/g
s/seven/7/g
s/eight/8/g
s/nine/9/g
s/ten/10/g
s/eleven/11/g
s/twelve/12/g
s/hundred/\*100+/g
s/thousand/)\*1000+(/g
s/million/)\*1000000+(/g
s/billion/)\*1000000000+(/g
s/trillion/)\*1000000000000+(/g
s/+)/)/g
s/+()//g
s/ //g' $1)

let n=$e
echo $n


Running it:
/path/where/you/saved/it <<<'Written number'
>>
>>56390555
If you have the patience to do this until 999 trillion then be my guest.
Smart people only have to know about 30 words.
>>
Haskell doesn't have any statements at all, much less if statements. Is that still okay?

import Data.Char(isAlpha)

unitsW = words "one two three four five six seven eight nine"
teensW = words "eleven twelve thirteen forteen fifteen " ++
words "sixteen seventeen eighteen nineteen"
tensW = words "ten twenty thirty forty fifty sixty seventy eighty ninety"
millsW = words "units thousand million billion trillion"

valsA = ("oh", 0) : concat [zip unitsW [1..], zip teensW [11..], zip tensW [10,20..]]
millsA = zip millsW (iterate (* 1000) 1)

val word = maybe undefined id (lookup word valsA)
mill word = maybe undefined id (lookup word millsA)

number = parse . lex
where lex = ("units" :) . reverse . words . map alphasOnly
parse = sum . map group . toGroups
group (power, words) = mill power * triple words
alphasOnly c = if isAlpha c then c else ' '


triple [] = 0
triple ("hundred" : words) = 100 * triple words
triple (word : words) = val word + triple words

toGroups [] = []
toGroups (power : rest) = (power, group) : toGroups rest'
where (group, rest') = break (`elem` millsW) rest

{-
*Main> number "one"
1
*Main> number "twenty"
20
*Main> number "three thousand"
3000
*Main> number "three thousand twenty-five"
3025
*Main> number "one million two hundred thousand"
1200000
-}


Also, fucking Haskell. The moment I got it to compile, it just worked, correctly.
>>
>>56390642
Err... one more sed line:

s/fif/5/g

Otherwise fifteen breaks the whole shit.

A good thing with this approach is it's language-agnostic. English "seventy-four", French "sixty-fourteen", German "fourandseventy", it's just about doing the right replacements.
>>
>>56386994
Can this be done with only 3 if statements for all integers? Even ones with hundreds of digits?
>>
>>56387000

I really cocked one up this morning, but it's the best way to determine baseline suitability for a programming role. Far better for both parties than those stupid "create a todo list in X" remote assignments.
>>
>>56390791
Pretty much, yeah.
As long as all the words that determine quantity, as in: hundred, thousand, million, billion etc
are all divisible by 100
The statements will always be:
1. < 100
2. == 100
3. > 100
>>
>>56387052

i would pay to see someone use this and going every case up to n just for shit and giggles.
>>
I lost all morning trying to solve it! (╬ ಠ益ಠ)
>>
BTW, how many of your programs would break with French?

70 = sixty-ten
79 = sixty-nineteen
80 = four-twenties
83 = four-twenties-three
90 = four-twenties-ten
97 = four-twenties-seventeen
>>
>>56390870
>French
Who care about French?
That language is irrelevant.
If the French don't want to learn English, I don't think the rest of the world want to learn their frog language.
>>
>>56390870
croissant
>>
>>56390889
Regardless of your idiotic comment (BTW I'm one who learned French), I'm wondering how different numbering systems would break the scripts.
>>
>>56391046
wonder it yourself french
>>
>>56387030
That's why employment agencies exist. Companies have a service contract with them, and just request staff as needed. Employment agency handles all the paperwork, takes a percentage, automatically paid by the company via a kronos system.
>>
>>56391069
I'm not French. Just learned it the same way I did with English, but my native language doesn't pose any kind of trouble for this kind of program.

Other languages that might break some algorithms:
* German - already mentioned, units go before the tens (74 = fourandseventy). Ordnung-dependent algorithms go nuts.
* Tzotzil - base 20. Nuff' said.
* Latin - numbers decline by case, and some numbers are made by subtraction (18 = twofromtwenty, 99 = onefromhundred)
>>
>>56391203
So you have an influx of low-wage programmers entering and leaving the company through a revolving door that all have to be trained to use your stack?

That's so wasteful.
>>
I think I'd first replace a few things in the string, like - with spaces.

Then I'd replace instances of "*teen" with "* ten"

Set up an orders of magnitude array, so " thousand" corresponds to 3 = n × 103, that sort of thing.

I'd loop through word by word (delim by space) from reverse.

Inside the loop would first be another loop that iterates over the order of magnitude array, seeing if that word is one of them, then storing a temporary OoM for later.

If found, we go next, if not, use a case for the first two letters of the word. This is the same for five as fifty, and each is unique. Only exception is the teens and ten, but we replaced those, so we only have the one exception. So that case statement converts it to whatever value.

Here's where we use one if statement: if the word ends in "ty", times it by ten. Add the final number to a temporary variable.

One last thing, after the OoM check, check if there's a temporary number in storage, or the last loop, and if so, multiply the old number by the old OoM and add to a final variable.

Hopefully explained the concept well enough. Two loops, one case select, one if.
>>
>>56391345
No you idiot, you get a few temp workers, see who sticks, offer them permanency.
>>
>>56391608
Yeah, I've been down that road before.
They keep jerking you around, saying they're gonna bring you on as a full employee if you do good, yet you keep running into temps working there 3 years who don't even have a fucking keycard to get around in the building because they don't have access like real employees and the only reason it's like this is because the company can avoid paying benefits on full-time employees.
>>
>>56387010
>What is open source?
>What is github?
>What is past experience

Solving bullshit whiteboard questions is the worst way to hire.

t. senior dev with 6 engineers
>>
>>56391652
Obviously you have to be smart about it. If you're useful to them, you have leverage; if you're not, you should have an exit strategy.
>>
>>56386994
presumably I can use a switch statement and demolish this problem. also i bet theres a library or framework that would do it automagically for me.
>>
>>56390870
Probably a lot, we have a retarded numbering system.

dix-huit milliards seize cent mille trois cent soixante seize
See all those edge cases?
>>
>>56391345
you had to be trained to use Slack? how did you manage to learn 4chan?
>>
File: shet up.jpg (44KB, 600x600px) Image search: [Google]
shet up.jpg
44KB, 600x600px
>>56386994
>solve this
>wait no, don't use tools! you're cheating!!!
>>
>>56386994
This sounds like one of those bullshit timewaster interviews where they bring you in and burn your whole day showing you around and then you get to the interview that they designed specifically for you to fail so they can run to the federal government and beg them for H1-B visas because WE CANT FIND ANY AMERICANS TO HIRE@!!!!!
>>
>>56386994
How high does it have to handle numbers? Do I also have to handle e.g. “four hundred ninety five million”?
>>
>>56387235
I like this. I'm a neophyte, so these solutions seem so simple while still being beyond me.

>>56387686
How come it doesn't work?
>>
Took me about 10 minutes of which most was writing out all of those stupid number names

{-# LANGUAGE ViewPatterns #-}

parseNum :: String -> Integer
parseNum = go 0 0 . words
where go t x [] = t + x
go t x ((bigMod -> Just m) : ws) = go (t + m * x) 0 ws
go t x ("hundred" : ws) = go t (100 * x) ws
go t x ((number -> Just n) : ws) = go t (x + n) ws

bigMod :: String -> Maybe Integer
bigMod = flip lookup
[ ("thousand", 1000)
, ("million", 1000000)
, ("billion", 1000000000)
-- ...
]

number :: String -> Maybe Integer
number = flip lookup
[ ("one", 1)
, ("two", 2)
, ("three", 3)
, ("four", 4)
, ("five", 5)
, ("six", 6)
, ("seven", 7)
, ("eight", 8)
, ("nine", 9)
, ("ten", 10)
, ("eleven", 11)
, ("twelve", 12)
, ("thirteen", 13)
, ("fourteen", 14)
, ("fifteen", 15)
, ("sixteen", 16)
, ("seventeen", 17)
, ("eighteen", 18)
, ("nineteen", 19)
, ("twenty", 20)
, ("thirty", 30)
, ("forty", 40)
, ("fifty", 50)
, ("sixty", 60)
, ("seventy", 70)
, ("eighty", 80)
, ("ninety", 90)
]


λ parseNum "three hundred fifty five million four hundred eighty nine thousand nine hundred eleven"
355489911



As an added bonus, also supports e.g. "fifty nine hundred".
>>
>>56390870
They're still single words, so there's no difference from e.g. "seventy" or "eighty" apart from the fact that you could get away with doing it slightly cheaper by recognizing them as separate words so you can re-use the entries for "nineteen" etc.
>>
>>56390735
No silly, I mean have indexes for 1 to 9, eleven to nineteen, hundred to however many digits you want, then index each digit
>>
>>56386994
If (x != 1033)
{
For (x = 0; x < 1033; ++x)
{
If (x ==1033)
{
Break;
}
}
}
>>
>>56392036
>implying lookup ain't many if
>>
>>56392596
>implying all of the code you compile doesn't compile down to shittons of branches either way
The problem's “if” count requirement is virtually impossible to solve, especially on a whiteboard, if taken transitively. Only real solution is to take it literally, and I literally used no “if”.

Anyway, depending on what you consider a branch, you could make it branchless by replacing every
if x then y else z
by essentially
(x) * (y) + (1-x) * (z)
.
>>
I'd google translate the written english number into japanese, since their writing of numbers has no irregular grammar: they straight up write numbers.

then make a lookup list of kanji for the 0-10, and then all the powers of ten, and convert it to numbers.

It helps that with more complex numbers, google translate simply writes them out in numbers, so if result is an integer, I can skip that lookup part.
>>
>>56392036
Does that work with various methods of writing thousands, such as "nineteen twenty seven" (the year 1927) or "nineteen hundred and twenty seven"?

Or for that matter will it work if I just write twenty-seven instead of twenty seven?
>>
>>56386994
Build a lookup table. No ifs needed.
>>
>>56387581
https://eeemo.net/
>>
>>56391872
Yes
>>
A lot of languages have int tostring that allows for language formats. Simply match the tostring with the input and return the appropriate int.
>>
>>56392783
If you were allowed to parse google translate, why not parse wolfram?
>>
>>56386994
you have to specify the requirements better and it's still a shitty problem
>>
>>56392783
don't call us, we'll call you
>>
>>56386994
rate

def string2int(number):
c019 = ["zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"]
c2x9x = ["","","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"]
c100 = ["","","hundred","thousand","","","million"]
current = 0
final = 0
for p in number.split():
if p in c019:
current += c019.index(p)
elif p in c2x9x:
current += c2x9x.index(p)*10
elif p in c100:
final += current * 10**c100.index(p)
current = 0

final += current
return final

print string2int(number)
>>
>>56392821
It splits on spaces only. Adding more splitting characters would be trivial, but not an essentially part of the task.

"nineteen twenty seven" would get parsed as 19+20+7. For 1927 you'd need to say "nineteen hundred twenty seven".

Changing this would be relatively straightforward, but I didn't consider it part of the question's scope because the question also made other assumptions about following a simple/regular format (e.g. no “and”) as well as there being no specification that it has to produce meaningful results or fail gracefully on “illegal” input.

Did I pass the interview?
>>
>>56386994
python:
table = {
"one": 1,
"two": 2,
"three": 3,
(...)
"one thousand and thirty three": 1033,
}

input = "one thousand and thirty three"
print table[input.lower()]

#three if's
if False:
if False:
if False:
pass

>>
>>56386994
>import framework
>done
If you're not neet you get shit done the easy way
>>
>>56386994
use not or
>>
Dictionary where the key is a string corresponding to each digit spelled out, the value is the digit itself.
For word in string.split() number += dict[word]*10^n where n is the loop counter.
Only works in languages with a string class/type and associative arrays/dictionaries. Python and JavaScript for example.
>>
You can use if statement once? Oh that is so cute.

var notIfStatement(control, trueHandler. falseHandler)
{
if(control)
{
return trueHandler;
}
else
{
return falseHandler;
}
}

If shitnozzle that made problem dare interject, they may want to be reminded there will be branching logic in the fucking libraries.
>>
File: qtt.png (129KB, 314x278px) Image search: [Google]
qtt.png
129KB, 314x278px
>>56386994
Can I use a vector of number strings and a for loop?
>>
Split the input into two distinct categories and define their behaviour.

First you have values "zero, one -> nineteen" and "twenty, thirty -> ninety" and then you have qualifiers, "hundred, thousand, million, billion"

You start reading word by word, modifying values by qualifiers.

That's how I'd solve it. (proceed to demonstrate on board, get job)
Thread posts: 150
Thread images: 8


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.