How many syllables in that number?
up vote
10
down vote
favorite
I'd love to take a number and know how many syllables are in it, when spoken in English.
Let's limit this to positive integers which are less than one thousand.
I'm British, so we're going to follow the hundreds column with an 'and' when there are any non-zero digits after it.
The Challenge
- Write some code which will accept a positive integer lower than 1000 and output the number of syllables in the words which represent that number in British English.
- It DOES NOT need to generate the words to represent the numbers, only the number of syllables they contain.
- It's code golf, attempt to achieve this in the fewest bytes.
- Use any language you like.
- The standard loopholes are forbidden.
Test Cases
| N | In words | Syllables |
| 1 | one | 1 |
| 2 | two | 1 |
| 3 | three | 1 |
| 4 | four | 1 |
| 5 | five | 1 |
| 6 | six | 1 |
| 7 | sev-en | 2 |
| 8 | eight | 1 |
| 9 | nine | 1 |
| 10 | ten | 1 |
| 11 | el-ev-en | 3 |
| 12 | twelve | 1 |
| 13 | thir-teen | 2 |
| 14 | four-teen | 2 |
| 17 | se-ven-teen | 3 |
| 20 | twen-ty | 2 |
| 21 | twen-ty one | 3 |
| 42 | four-ty two | 3 |
| 73 | sev-en-ty three | 4 |
| 77 | sev-en-ty sev-en | 5 |
| 100 | one hund-red | 3 |
| 110 | one hund-red and ten | 5 |
| 111 | one hund-red and el-ev-en | 7 |
| 555 | five hund-red and fif-ty five | 7 |
| 700 | sev-en hund-red | 4 |
| 770 | sev-en hund-red and sev-en-ty | 8 |
| 777 | sev-en hund-red and sev-en-ty sev-en | 10 |
| 999 | nine hund-red and nine-ty nine | 7 |
code-golf natural-language
add a comment |
up vote
10
down vote
favorite
I'd love to take a number and know how many syllables are in it, when spoken in English.
Let's limit this to positive integers which are less than one thousand.
I'm British, so we're going to follow the hundreds column with an 'and' when there are any non-zero digits after it.
The Challenge
- Write some code which will accept a positive integer lower than 1000 and output the number of syllables in the words which represent that number in British English.
- It DOES NOT need to generate the words to represent the numbers, only the number of syllables they contain.
- It's code golf, attempt to achieve this in the fewest bytes.
- Use any language you like.
- The standard loopholes are forbidden.
Test Cases
| N | In words | Syllables |
| 1 | one | 1 |
| 2 | two | 1 |
| 3 | three | 1 |
| 4 | four | 1 |
| 5 | five | 1 |
| 6 | six | 1 |
| 7 | sev-en | 2 |
| 8 | eight | 1 |
| 9 | nine | 1 |
| 10 | ten | 1 |
| 11 | el-ev-en | 3 |
| 12 | twelve | 1 |
| 13 | thir-teen | 2 |
| 14 | four-teen | 2 |
| 17 | se-ven-teen | 3 |
| 20 | twen-ty | 2 |
| 21 | twen-ty one | 3 |
| 42 | four-ty two | 3 |
| 73 | sev-en-ty three | 4 |
| 77 | sev-en-ty sev-en | 5 |
| 100 | one hund-red | 3 |
| 110 | one hund-red and ten | 5 |
| 111 | one hund-red and el-ev-en | 7 |
| 555 | five hund-red and fif-ty five | 7 |
| 700 | sev-en hund-red | 4 |
| 770 | sev-en hund-red and sev-en-ty | 8 |
| 777 | sev-en hund-red and sev-en-ty sev-en | 10 |
| 999 | nine hund-red and nine-ty nine | 7 |
code-golf natural-language
Can we take input as a string or an array of digits?
– Dennis♦
14 hours ago
add a comment |
up vote
10
down vote
favorite
up vote
10
down vote
favorite
I'd love to take a number and know how many syllables are in it, when spoken in English.
Let's limit this to positive integers which are less than one thousand.
I'm British, so we're going to follow the hundreds column with an 'and' when there are any non-zero digits after it.
The Challenge
- Write some code which will accept a positive integer lower than 1000 and output the number of syllables in the words which represent that number in British English.
- It DOES NOT need to generate the words to represent the numbers, only the number of syllables they contain.
- It's code golf, attempt to achieve this in the fewest bytes.
- Use any language you like.
- The standard loopholes are forbidden.
Test Cases
| N | In words | Syllables |
| 1 | one | 1 |
| 2 | two | 1 |
| 3 | three | 1 |
| 4 | four | 1 |
| 5 | five | 1 |
| 6 | six | 1 |
| 7 | sev-en | 2 |
| 8 | eight | 1 |
| 9 | nine | 1 |
| 10 | ten | 1 |
| 11 | el-ev-en | 3 |
| 12 | twelve | 1 |
| 13 | thir-teen | 2 |
| 14 | four-teen | 2 |
| 17 | se-ven-teen | 3 |
| 20 | twen-ty | 2 |
| 21 | twen-ty one | 3 |
| 42 | four-ty two | 3 |
| 73 | sev-en-ty three | 4 |
| 77 | sev-en-ty sev-en | 5 |
| 100 | one hund-red | 3 |
| 110 | one hund-red and ten | 5 |
| 111 | one hund-red and el-ev-en | 7 |
| 555 | five hund-red and fif-ty five | 7 |
| 700 | sev-en hund-red | 4 |
| 770 | sev-en hund-red and sev-en-ty | 8 |
| 777 | sev-en hund-red and sev-en-ty sev-en | 10 |
| 999 | nine hund-red and nine-ty nine | 7 |
code-golf natural-language
I'd love to take a number and know how many syllables are in it, when spoken in English.
Let's limit this to positive integers which are less than one thousand.
I'm British, so we're going to follow the hundreds column with an 'and' when there are any non-zero digits after it.
The Challenge
- Write some code which will accept a positive integer lower than 1000 and output the number of syllables in the words which represent that number in British English.
- It DOES NOT need to generate the words to represent the numbers, only the number of syllables they contain.
- It's code golf, attempt to achieve this in the fewest bytes.
- Use any language you like.
- The standard loopholes are forbidden.
Test Cases
| N | In words | Syllables |
| 1 | one | 1 |
| 2 | two | 1 |
| 3 | three | 1 |
| 4 | four | 1 |
| 5 | five | 1 |
| 6 | six | 1 |
| 7 | sev-en | 2 |
| 8 | eight | 1 |
| 9 | nine | 1 |
| 10 | ten | 1 |
| 11 | el-ev-en | 3 |
| 12 | twelve | 1 |
| 13 | thir-teen | 2 |
| 14 | four-teen | 2 |
| 17 | se-ven-teen | 3 |
| 20 | twen-ty | 2 |
| 21 | twen-ty one | 3 |
| 42 | four-ty two | 3 |
| 73 | sev-en-ty three | 4 |
| 77 | sev-en-ty sev-en | 5 |
| 100 | one hund-red | 3 |
| 110 | one hund-red and ten | 5 |
| 111 | one hund-red and el-ev-en | 7 |
| 555 | five hund-red and fif-ty five | 7 |
| 700 | sev-en hund-red | 4 |
| 770 | sev-en hund-red and sev-en-ty | 8 |
| 777 | sev-en hund-red and sev-en-ty sev-en | 10 |
| 999 | nine hund-red and nine-ty nine | 7 |
code-golf natural-language
code-golf natural-language
edited 15 hours ago
asked 17 hours ago
AJFaraday
3,19142854
3,19142854
Can we take input as a string or an array of digits?
– Dennis♦
14 hours ago
add a comment |
Can we take input as a string or an array of digits?
– Dennis♦
14 hours ago
Can we take input as a string or an array of digits?
– Dennis♦
14 hours ago
Can we take input as a string or an array of digits?
– Dennis♦
14 hours ago
add a comment |
11 Answers
11
active
oldest
votes
up vote
5
down vote
Python 2, 112 bytes
f=lambda n:n>99and f(n/100)+3+f(n%100)-(n%100<1)or n>19and f(n/10)+1+f(n%10)or([0]+[1]*6+[2,1,1,1,3,1]+[2]*7)[n]
Try it online!
2
Also, your[2]*7
part will fail for17
, since that should be 3 instead of 2 (sev-en-teen
).
– Kevin Cruijssen
15 hours ago
1
-4 bytes, including a fix for 17.
– Shaggy
12 hours ago
add a comment |
up vote
5
down vote
Python 2, 84 83 74 67 bytes
lambda n:4*(n>99)+2-n%~9/9-0x55561aaaab/4**(n%100)%4+`n`.count('7')
Thanks to @xnor for golfing off 9 16 bytes!
Try it online!
Python 2, 79 bytes
lambda n:4*(n>99)+([-1]+10*[1]+[3,1]+7*[2]+8*([2]+9*[3]))[n%100]+`n`.count('7')
Straightforward, but longer.
Try it online!
For your 83-byte solution, you can cut 3 bytes by changing-10
to~9
and switching around the last bit to+(0<n%100!=12)-(n%100!=11)
, but that's still longer than your new solution.
– xnor
9 hours ago
Here we go:lambda n:4*(n>99)+`n`.count('7')+cmp(n/10%10,1)-n%~9/9-min(n%100,13)%12/~9
– xnor
9 hours ago
@xnor That's really clever!min(n%100,13)%12/~9
might actually help with an approach I was trying for my Jelly answer as well.
– Dennis♦
6 hours ago
Actually, just shoving things into a hardcoded constant turns out shorter.
– xnor
6 hours ago
@xnor Thanks again!
– Dennis♦
6 hours ago
add a comment |
up vote
4
down vote
05AB1E, 34 31 bytes
т%U7¢I€Ā`Iт@3*X_(X20@X12Q(X11QO
Try it online or verify all [1,999]
test cases.
Explanation:
With all checks mentioned it will result in 1 for truthy and 0 for falsey.
т% # Take modulo-100 of the (implicit) input
# i.e. 710 → 10
U # Pop and store it in variable `X`
7¢ # Count the amount of 7s in the (implicit) input
# i.e. 710 → 1
I€Ā # Trutify each digit in the input (0 if 0; 1 otherwise)
` # And push all of the mapped values to the stack
# i.e. 710 → [1,1,0]
Iт@ # Check if the input is larger than or equal to 100
# i.e. 710 → 1 (truthy)
3* # Multiply that result by 3 (for 'hund-red and')
# i.e. 1 → 3
X_ # Check if variable `X` is 0
# i.e. 10 → 0 (falsey)
( # And negate that (to remove 'and' when #00)
# i.e. 0 → 0
X20@ # Check if variable `X` is larger than or equal to 20 (for '-ty')
# i.e. 10 → 0 (falsey)
X12Q # Check if variable `X` is exactly 12
# i.e. 10 → 0 (falsey)
( # And negate that (to remove 'teen')
# i.e. 0 → 0
X11Q # Check if variable `X` is exactly 11 (for 'el-ev-en' minus 'one one')
# i.e. 10 → 0 (falsey)
O # Sum everything on the stack (and output implicitly)
# i.e. [1,1,1,0,3,0,0,0,0] → 6
This fails the 700 test case. 'Seven Hundred' has 4 syllables, this returns 5.
– AJFaraday
16 hours ago
@AJFaraday Should be fixed now. Accidentally hadI
(input) instead ofX
(input mod 100) when checking if it's larger than 20 for the +1 ofty
.
– Kevin Cruijssen
16 hours ago
I'm so sorry, it returns 0 for 'one hundred'
– AJFaraday
16 hours ago
@AJFaraday Fixed again..>
(check if input is larger than 100) has been replaced with@
(check if input is larger than or equal to 100). Maybe I should have checked some more test cases myself more carefully before posting.. Sorry about that..
– Kevin Cruijssen
16 hours ago
4
By the way, loving the top hat on a rubix cube!
– AJFaraday
16 hours ago
|
show 2 more comments
up vote
4
down vote
JavaScript (ES6), 89 bytes
n=>(s='01111112111312222322',n>99&&+s[n/100|0]+3-!(n%=100))+~~(s[n]||+s[n/10|0]-~s[n%10])
Try it online!
add a comment |
up vote
2
down vote
Jelly, 28 25 23 bytes
9ḊŻ;2+⁵Żċ%ȷ2$ạDṠḄƊ+Dċ7Ɗ
Try it online!
How it works
9ḊŻ;2+⁵Żċ%ȷ2$ạDṠḄƊ+Dċ7Ɗ Main link. Argument: n (integer in [1, ..., 999])
9 Set the return value to 9.
Ḋ Dequeue; yield [2, 3, 4, 5, 6, 7, 8, 9].
Ż Zero; yield [0, 2, 3, 4, 5, 6, 7, 8, 9].
;2 Concat 2, yield [0, 2, 3, 4, 5, 6, 7, 8, 9, 2].
+⁵ Add 10; yield [10, 12, 13, 14, 15, 16, 17, 18, 19, 12].
Ż Zero; yield [0, 10, 12, 13, 14, 15, 16, 17, 18, 19, 12].
%ȷ2$ Yield n % 1e2.
ċ Count the occurrences of the modulus in the array.
Ɗ Combine the three links to the left into a monadic chain.
D Decimal; convert n to its array of digits in base 10.
Ṡ Take the sign of each decimal digit (0 or 1).
Ḅ Convert the array of signs from base 2 to integer.
ạ Compute the abs. difference of the results to both sides.
Ɗ Combine the three links to the left into a monadic chain.
D Decimal; convert n to its array of digits in base 10.
ċ7 Count the number of 7's.
add a comment |
up vote
2
down vote
JavaScript, 86 84 bytes
A tweaked port of TFeld's Python solution.
f=n=>n>99?f(n/100)+3+f(n%=100)-!n:n>19?f(n/10)-~f(n%10):+"01111112111312222322"[n|0]
Try it online
2 bytes saved thanks to Arnauld
add a comment |
up vote
2
down vote
Java 11, 105 bytes
n->(""+"".repeat(8)).charAt(n%100)+(n+"").split("7",-1).length-6+(n>99?4:0)
Contains loads of unprintable characters.
Try it online.
Explanation:
n-> // Method with integer as both parameter and return-type
("" // Push string with ASCII-value digits 46666666666867777777
+"".repeat(8)) // Appended with 8 times a string with ASCII-value digits 7888888888
.charAt(n%100) // Take the (input modulo-100)'th character of this string (as integer)
+(n+"").split("7",-1).length
// Count the amount of 7s in the input + 1
-6 // Subtract 6 (1 for the 7s+1 count and 5 to map the ASCII-digits to:
// 4 → -1; 6 → 1; 7 → 2; 8 → 3)
+(n>99? // And if the input is larger than 99:
4 // Add 4
: // Else:
0) // Leave it the same by adding 0
Fyi, the TIO-generated PPCG answer would include the unprintables as well, which SE strips otherwise.
– Dennis♦
6 hours ago
add a comment |
up vote
0
down vote
Charcoal, 39 bytes
≔﹪N¹⁰⁰ηI⁻⁻⁻⁺⁺↨E謬Iι²№θ7⁼η¹¹⁼η¹²¬η⁼¹÷ηχ
Try it online! Link is to verbose version of code. This is an arithmetic solution. Explanation:
≔﹪N¹⁰⁰η
Take the input number modulo 100.
I⁻⁻⁻⁺⁺
Calculate adjustments to the number of syllables and output the result as a string.
↨E謬Iι²
Start by changing each non-zero digit to 1 and then decoding as base 2. This gives the correct answer for most inputs.
№θ7
Add 1 for each 7
.
⁼η¹¹
Add 1 if the number ends in 11
.
⁼η¹²
Subtract 1 if the number ends in 12
.
¬η
Subtract 1 if the number ends in 00
.
⁼¹÷ηχ
Subtract 1 if the number ends in 1X
(where X is any digit including 1
and 2
).
add a comment |
up vote
0
down vote
Perl 5 -pF
, 53 bytes
$_=4*/.../+2*/[^0].$/+!/0$/+y/7//-/1[^1]$/-/12$/-/00/
Try it online!
How
$_=4*/.../ # Hundreds place has minimum of 4 sylables (__ HUN-DRED AND),
# match fails on number <100, and would add 0 here
+2*/[^0].$/ # Tens place has two syllables if not 0 (__-TY or __TEEN),
# match fails on numbers <10, and would add 0
+!/0$/ # Ones place has one syllable if not 0 (__)
# -- Now adjust for special cases --
+y/7// # add a syllable for every 7 present
-/1[^1]$/ # remove a syllable for 10-19, except 11
-/12$/ # remove another syllable for 12
-/00/ # remove the syllable for AND if it's an even hundred
add a comment |
up vote
0
down vote
PHP, 158 bytes
<?php $i=fgets(STDIN);for($j=$p=0;$p<strlen($i);){!str_split($i)[$p++]>0?:++$j;};echo substr_count($i,7)+$j+3*($i>=100)-!($i%=100)+($i>=20)-($i==12)+($i==11);
Try it online!
A port of Kevin Cruijssen's solution (unfortunately it doesn't have the same brevity in PHP :) )
-32 thanks to Shaggy!
So many savings to be made here! Here's just a few very quick ones
– Shaggy
5 hours ago
add a comment |
up vote
0
down vote
Wolfram Language 101 Bytes
s = StringSplit;Length[Join@@(WordData[#,"Hyphenation"]&/@
Join@@s/@s[IntegerName@#,"-"])]+Boole[#>100]&
Explanation
(substituting StringSplit
for s
)
Length[Join@@(WordData[#,"Hyphenation"]&/@
Join@@StringSplit/@StringSplit[IntegerName@#,"-"])]+Boole[#>100]&
IntegerName
renders the number in American English (i.e. without "and" included in numbers greater than 100.) E.g. 777-> "seven hundred seventy-seven
.
StringSplit[IntegerName@#,"-"]
removes any hyphens in the rendering.
StringSplit/@
splits the rendering into words.
Join@@
leaves a simple list of words, without embedded list (in the case that a hyphen appeared).
WordData[#,"Hyphenation"]
breaks up a single word into its syllables.
Join@@
leaves a simple list of syllables in all of the words.
Length
counts the syllables
+Boole[#>100]
adds 1
to the syllable count for those numbers greater than 100 (because of the additional "and" employed in the British English rendering).
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f177586%2fhow-many-syllables-in-that-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
11 Answers
11
active
oldest
votes
11 Answers
11
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
Python 2, 112 bytes
f=lambda n:n>99and f(n/100)+3+f(n%100)-(n%100<1)or n>19and f(n/10)+1+f(n%10)or([0]+[1]*6+[2,1,1,1,3,1]+[2]*7)[n]
Try it online!
2
Also, your[2]*7
part will fail for17
, since that should be 3 instead of 2 (sev-en-teen
).
– Kevin Cruijssen
15 hours ago
1
-4 bytes, including a fix for 17.
– Shaggy
12 hours ago
add a comment |
up vote
5
down vote
Python 2, 112 bytes
f=lambda n:n>99and f(n/100)+3+f(n%100)-(n%100<1)or n>19and f(n/10)+1+f(n%10)or([0]+[1]*6+[2,1,1,1,3,1]+[2]*7)[n]
Try it online!
2
Also, your[2]*7
part will fail for17
, since that should be 3 instead of 2 (sev-en-teen
).
– Kevin Cruijssen
15 hours ago
1
-4 bytes, including a fix for 17.
– Shaggy
12 hours ago
add a comment |
up vote
5
down vote
up vote
5
down vote
Python 2, 112 bytes
f=lambda n:n>99and f(n/100)+3+f(n%100)-(n%100<1)or n>19and f(n/10)+1+f(n%10)or([0]+[1]*6+[2,1,1,1,3,1]+[2]*7)[n]
Try it online!
Python 2, 112 bytes
f=lambda n:n>99and f(n/100)+3+f(n%100)-(n%100<1)or n>19and f(n/10)+1+f(n%10)or([0]+[1]*6+[2,1,1,1,3,1]+[2]*7)[n]
Try it online!
answered 16 hours ago
TFeld
14.1k21240
14.1k21240
2
Also, your[2]*7
part will fail for17
, since that should be 3 instead of 2 (sev-en-teen
).
– Kevin Cruijssen
15 hours ago
1
-4 bytes, including a fix for 17.
– Shaggy
12 hours ago
add a comment |
2
Also, your[2]*7
part will fail for17
, since that should be 3 instead of 2 (sev-en-teen
).
– Kevin Cruijssen
15 hours ago
1
-4 bytes, including a fix for 17.
– Shaggy
12 hours ago
2
2
Also, your
[2]*7
part will fail for 17
, since that should be 3 instead of 2 (sev-en-teen
).– Kevin Cruijssen
15 hours ago
Also, your
[2]*7
part will fail for 17
, since that should be 3 instead of 2 (sev-en-teen
).– Kevin Cruijssen
15 hours ago
1
1
-4 bytes, including a fix for 17.
– Shaggy
12 hours ago
-4 bytes, including a fix for 17.
– Shaggy
12 hours ago
add a comment |
up vote
5
down vote
Python 2, 84 83 74 67 bytes
lambda n:4*(n>99)+2-n%~9/9-0x55561aaaab/4**(n%100)%4+`n`.count('7')
Thanks to @xnor for golfing off 9 16 bytes!
Try it online!
Python 2, 79 bytes
lambda n:4*(n>99)+([-1]+10*[1]+[3,1]+7*[2]+8*([2]+9*[3]))[n%100]+`n`.count('7')
Straightforward, but longer.
Try it online!
For your 83-byte solution, you can cut 3 bytes by changing-10
to~9
and switching around the last bit to+(0<n%100!=12)-(n%100!=11)
, but that's still longer than your new solution.
– xnor
9 hours ago
Here we go:lambda n:4*(n>99)+`n`.count('7')+cmp(n/10%10,1)-n%~9/9-min(n%100,13)%12/~9
– xnor
9 hours ago
@xnor That's really clever!min(n%100,13)%12/~9
might actually help with an approach I was trying for my Jelly answer as well.
– Dennis♦
6 hours ago
Actually, just shoving things into a hardcoded constant turns out shorter.
– xnor
6 hours ago
@xnor Thanks again!
– Dennis♦
6 hours ago
add a comment |
up vote
5
down vote
Python 2, 84 83 74 67 bytes
lambda n:4*(n>99)+2-n%~9/9-0x55561aaaab/4**(n%100)%4+`n`.count('7')
Thanks to @xnor for golfing off 9 16 bytes!
Try it online!
Python 2, 79 bytes
lambda n:4*(n>99)+([-1]+10*[1]+[3,1]+7*[2]+8*([2]+9*[3]))[n%100]+`n`.count('7')
Straightforward, but longer.
Try it online!
For your 83-byte solution, you can cut 3 bytes by changing-10
to~9
and switching around the last bit to+(0<n%100!=12)-(n%100!=11)
, but that's still longer than your new solution.
– xnor
9 hours ago
Here we go:lambda n:4*(n>99)+`n`.count('7')+cmp(n/10%10,1)-n%~9/9-min(n%100,13)%12/~9
– xnor
9 hours ago
@xnor That's really clever!min(n%100,13)%12/~9
might actually help with an approach I was trying for my Jelly answer as well.
– Dennis♦
6 hours ago
Actually, just shoving things into a hardcoded constant turns out shorter.
– xnor
6 hours ago
@xnor Thanks again!
– Dennis♦
6 hours ago
add a comment |
up vote
5
down vote
up vote
5
down vote
Python 2, 84 83 74 67 bytes
lambda n:4*(n>99)+2-n%~9/9-0x55561aaaab/4**(n%100)%4+`n`.count('7')
Thanks to @xnor for golfing off 9 16 bytes!
Try it online!
Python 2, 79 bytes
lambda n:4*(n>99)+([-1]+10*[1]+[3,1]+7*[2]+8*([2]+9*[3]))[n%100]+`n`.count('7')
Straightforward, but longer.
Try it online!
Python 2, 84 83 74 67 bytes
lambda n:4*(n>99)+2-n%~9/9-0x55561aaaab/4**(n%100)%4+`n`.count('7')
Thanks to @xnor for golfing off 9 16 bytes!
Try it online!
Python 2, 79 bytes
lambda n:4*(n>99)+([-1]+10*[1]+[3,1]+7*[2]+8*([2]+9*[3]))[n%100]+`n`.count('7')
Straightforward, but longer.
Try it online!
edited 6 hours ago
answered 13 hours ago
Dennis♦
186k32295734
186k32295734
For your 83-byte solution, you can cut 3 bytes by changing-10
to~9
and switching around the last bit to+(0<n%100!=12)-(n%100!=11)
, but that's still longer than your new solution.
– xnor
9 hours ago
Here we go:lambda n:4*(n>99)+`n`.count('7')+cmp(n/10%10,1)-n%~9/9-min(n%100,13)%12/~9
– xnor
9 hours ago
@xnor That's really clever!min(n%100,13)%12/~9
might actually help with an approach I was trying for my Jelly answer as well.
– Dennis♦
6 hours ago
Actually, just shoving things into a hardcoded constant turns out shorter.
– xnor
6 hours ago
@xnor Thanks again!
– Dennis♦
6 hours ago
add a comment |
For your 83-byte solution, you can cut 3 bytes by changing-10
to~9
and switching around the last bit to+(0<n%100!=12)-(n%100!=11)
, but that's still longer than your new solution.
– xnor
9 hours ago
Here we go:lambda n:4*(n>99)+`n`.count('7')+cmp(n/10%10,1)-n%~9/9-min(n%100,13)%12/~9
– xnor
9 hours ago
@xnor That's really clever!min(n%100,13)%12/~9
might actually help with an approach I was trying for my Jelly answer as well.
– Dennis♦
6 hours ago
Actually, just shoving things into a hardcoded constant turns out shorter.
– xnor
6 hours ago
@xnor Thanks again!
– Dennis♦
6 hours ago
For your 83-byte solution, you can cut 3 bytes by changing
-10
to ~9
and switching around the last bit to +(0<n%100!=12)-(n%100!=11)
, but that's still longer than your new solution.– xnor
9 hours ago
For your 83-byte solution, you can cut 3 bytes by changing
-10
to ~9
and switching around the last bit to +(0<n%100!=12)-(n%100!=11)
, but that's still longer than your new solution.– xnor
9 hours ago
Here we go:
lambda n:4*(n>99)+`n`.count('7')+cmp(n/10%10,1)-n%~9/9-min(n%100,13)%12/~9
– xnor
9 hours ago
Here we go:
lambda n:4*(n>99)+`n`.count('7')+cmp(n/10%10,1)-n%~9/9-min(n%100,13)%12/~9
– xnor
9 hours ago
@xnor That's really clever!
min(n%100,13)%12/~9
might actually help with an approach I was trying for my Jelly answer as well.– Dennis♦
6 hours ago
@xnor That's really clever!
min(n%100,13)%12/~9
might actually help with an approach I was trying for my Jelly answer as well.– Dennis♦
6 hours ago
Actually, just shoving things into a hardcoded constant turns out shorter.
– xnor
6 hours ago
Actually, just shoving things into a hardcoded constant turns out shorter.
– xnor
6 hours ago
@xnor Thanks again!
– Dennis♦
6 hours ago
@xnor Thanks again!
– Dennis♦
6 hours ago
add a comment |
up vote
4
down vote
05AB1E, 34 31 bytes
т%U7¢I€Ā`Iт@3*X_(X20@X12Q(X11QO
Try it online or verify all [1,999]
test cases.
Explanation:
With all checks mentioned it will result in 1 for truthy and 0 for falsey.
т% # Take modulo-100 of the (implicit) input
# i.e. 710 → 10
U # Pop and store it in variable `X`
7¢ # Count the amount of 7s in the (implicit) input
# i.e. 710 → 1
I€Ā # Trutify each digit in the input (0 if 0; 1 otherwise)
` # And push all of the mapped values to the stack
# i.e. 710 → [1,1,0]
Iт@ # Check if the input is larger than or equal to 100
# i.e. 710 → 1 (truthy)
3* # Multiply that result by 3 (for 'hund-red and')
# i.e. 1 → 3
X_ # Check if variable `X` is 0
# i.e. 10 → 0 (falsey)
( # And negate that (to remove 'and' when #00)
# i.e. 0 → 0
X20@ # Check if variable `X` is larger than or equal to 20 (for '-ty')
# i.e. 10 → 0 (falsey)
X12Q # Check if variable `X` is exactly 12
# i.e. 10 → 0 (falsey)
( # And negate that (to remove 'teen')
# i.e. 0 → 0
X11Q # Check if variable `X` is exactly 11 (for 'el-ev-en' minus 'one one')
# i.e. 10 → 0 (falsey)
O # Sum everything on the stack (and output implicitly)
# i.e. [1,1,1,0,3,0,0,0,0] → 6
This fails the 700 test case. 'Seven Hundred' has 4 syllables, this returns 5.
– AJFaraday
16 hours ago
@AJFaraday Should be fixed now. Accidentally hadI
(input) instead ofX
(input mod 100) when checking if it's larger than 20 for the +1 ofty
.
– Kevin Cruijssen
16 hours ago
I'm so sorry, it returns 0 for 'one hundred'
– AJFaraday
16 hours ago
@AJFaraday Fixed again..>
(check if input is larger than 100) has been replaced with@
(check if input is larger than or equal to 100). Maybe I should have checked some more test cases myself more carefully before posting.. Sorry about that..
– Kevin Cruijssen
16 hours ago
4
By the way, loving the top hat on a rubix cube!
– AJFaraday
16 hours ago
|
show 2 more comments
up vote
4
down vote
05AB1E, 34 31 bytes
т%U7¢I€Ā`Iт@3*X_(X20@X12Q(X11QO
Try it online or verify all [1,999]
test cases.
Explanation:
With all checks mentioned it will result in 1 for truthy and 0 for falsey.
т% # Take modulo-100 of the (implicit) input
# i.e. 710 → 10
U # Pop and store it in variable `X`
7¢ # Count the amount of 7s in the (implicit) input
# i.e. 710 → 1
I€Ā # Trutify each digit in the input (0 if 0; 1 otherwise)
` # And push all of the mapped values to the stack
# i.e. 710 → [1,1,0]
Iт@ # Check if the input is larger than or equal to 100
# i.e. 710 → 1 (truthy)
3* # Multiply that result by 3 (for 'hund-red and')
# i.e. 1 → 3
X_ # Check if variable `X` is 0
# i.e. 10 → 0 (falsey)
( # And negate that (to remove 'and' when #00)
# i.e. 0 → 0
X20@ # Check if variable `X` is larger than or equal to 20 (for '-ty')
# i.e. 10 → 0 (falsey)
X12Q # Check if variable `X` is exactly 12
# i.e. 10 → 0 (falsey)
( # And negate that (to remove 'teen')
# i.e. 0 → 0
X11Q # Check if variable `X` is exactly 11 (for 'el-ev-en' minus 'one one')
# i.e. 10 → 0 (falsey)
O # Sum everything on the stack (and output implicitly)
# i.e. [1,1,1,0,3,0,0,0,0] → 6
This fails the 700 test case. 'Seven Hundred' has 4 syllables, this returns 5.
– AJFaraday
16 hours ago
@AJFaraday Should be fixed now. Accidentally hadI
(input) instead ofX
(input mod 100) when checking if it's larger than 20 for the +1 ofty
.
– Kevin Cruijssen
16 hours ago
I'm so sorry, it returns 0 for 'one hundred'
– AJFaraday
16 hours ago
@AJFaraday Fixed again..>
(check if input is larger than 100) has been replaced with@
(check if input is larger than or equal to 100). Maybe I should have checked some more test cases myself more carefully before posting.. Sorry about that..
– Kevin Cruijssen
16 hours ago
4
By the way, loving the top hat on a rubix cube!
– AJFaraday
16 hours ago
|
show 2 more comments
up vote
4
down vote
up vote
4
down vote
05AB1E, 34 31 bytes
т%U7¢I€Ā`Iт@3*X_(X20@X12Q(X11QO
Try it online or verify all [1,999]
test cases.
Explanation:
With all checks mentioned it will result in 1 for truthy and 0 for falsey.
т% # Take modulo-100 of the (implicit) input
# i.e. 710 → 10
U # Pop and store it in variable `X`
7¢ # Count the amount of 7s in the (implicit) input
# i.e. 710 → 1
I€Ā # Trutify each digit in the input (0 if 0; 1 otherwise)
` # And push all of the mapped values to the stack
# i.e. 710 → [1,1,0]
Iт@ # Check if the input is larger than or equal to 100
# i.e. 710 → 1 (truthy)
3* # Multiply that result by 3 (for 'hund-red and')
# i.e. 1 → 3
X_ # Check if variable `X` is 0
# i.e. 10 → 0 (falsey)
( # And negate that (to remove 'and' when #00)
# i.e. 0 → 0
X20@ # Check if variable `X` is larger than or equal to 20 (for '-ty')
# i.e. 10 → 0 (falsey)
X12Q # Check if variable `X` is exactly 12
# i.e. 10 → 0 (falsey)
( # And negate that (to remove 'teen')
# i.e. 0 → 0
X11Q # Check if variable `X` is exactly 11 (for 'el-ev-en' minus 'one one')
# i.e. 10 → 0 (falsey)
O # Sum everything on the stack (and output implicitly)
# i.e. [1,1,1,0,3,0,0,0,0] → 6
05AB1E, 34 31 bytes
т%U7¢I€Ā`Iт@3*X_(X20@X12Q(X11QO
Try it online or verify all [1,999]
test cases.
Explanation:
With all checks mentioned it will result in 1 for truthy and 0 for falsey.
т% # Take modulo-100 of the (implicit) input
# i.e. 710 → 10
U # Pop and store it in variable `X`
7¢ # Count the amount of 7s in the (implicit) input
# i.e. 710 → 1
I€Ā # Trutify each digit in the input (0 if 0; 1 otherwise)
` # And push all of the mapped values to the stack
# i.e. 710 → [1,1,0]
Iт@ # Check if the input is larger than or equal to 100
# i.e. 710 → 1 (truthy)
3* # Multiply that result by 3 (for 'hund-red and')
# i.e. 1 → 3
X_ # Check if variable `X` is 0
# i.e. 10 → 0 (falsey)
( # And negate that (to remove 'and' when #00)
# i.e. 0 → 0
X20@ # Check if variable `X` is larger than or equal to 20 (for '-ty')
# i.e. 10 → 0 (falsey)
X12Q # Check if variable `X` is exactly 12
# i.e. 10 → 0 (falsey)
( # And negate that (to remove 'teen')
# i.e. 0 → 0
X11Q # Check if variable `X` is exactly 11 (for 'el-ev-en' minus 'one one')
# i.e. 10 → 0 (falsey)
O # Sum everything on the stack (and output implicitly)
# i.e. [1,1,1,0,3,0,0,0,0] → 6
edited 8 hours ago
answered 16 hours ago
Kevin Cruijssen
35.4k554186
35.4k554186
This fails the 700 test case. 'Seven Hundred' has 4 syllables, this returns 5.
– AJFaraday
16 hours ago
@AJFaraday Should be fixed now. Accidentally hadI
(input) instead ofX
(input mod 100) when checking if it's larger than 20 for the +1 ofty
.
– Kevin Cruijssen
16 hours ago
I'm so sorry, it returns 0 for 'one hundred'
– AJFaraday
16 hours ago
@AJFaraday Fixed again..>
(check if input is larger than 100) has been replaced with@
(check if input is larger than or equal to 100). Maybe I should have checked some more test cases myself more carefully before posting.. Sorry about that..
– Kevin Cruijssen
16 hours ago
4
By the way, loving the top hat on a rubix cube!
– AJFaraday
16 hours ago
|
show 2 more comments
This fails the 700 test case. 'Seven Hundred' has 4 syllables, this returns 5.
– AJFaraday
16 hours ago
@AJFaraday Should be fixed now. Accidentally hadI
(input) instead ofX
(input mod 100) when checking if it's larger than 20 for the +1 ofty
.
– Kevin Cruijssen
16 hours ago
I'm so sorry, it returns 0 for 'one hundred'
– AJFaraday
16 hours ago
@AJFaraday Fixed again..>
(check if input is larger than 100) has been replaced with@
(check if input is larger than or equal to 100). Maybe I should have checked some more test cases myself more carefully before posting.. Sorry about that..
– Kevin Cruijssen
16 hours ago
4
By the way, loving the top hat on a rubix cube!
– AJFaraday
16 hours ago
This fails the 700 test case. 'Seven Hundred' has 4 syllables, this returns 5.
– AJFaraday
16 hours ago
This fails the 700 test case. 'Seven Hundred' has 4 syllables, this returns 5.
– AJFaraday
16 hours ago
@AJFaraday Should be fixed now. Accidentally had
I
(input) instead of X
(input mod 100) when checking if it's larger than 20 for the +1 of ty
.– Kevin Cruijssen
16 hours ago
@AJFaraday Should be fixed now. Accidentally had
I
(input) instead of X
(input mod 100) when checking if it's larger than 20 for the +1 of ty
.– Kevin Cruijssen
16 hours ago
I'm so sorry, it returns 0 for 'one hundred'
– AJFaraday
16 hours ago
I'm so sorry, it returns 0 for 'one hundred'
– AJFaraday
16 hours ago
@AJFaraday Fixed again..
>
(check if input is larger than 100) has been replaced with @
(check if input is larger than or equal to 100). Maybe I should have checked some more test cases myself more carefully before posting.. Sorry about that..– Kevin Cruijssen
16 hours ago
@AJFaraday Fixed again..
>
(check if input is larger than 100) has been replaced with @
(check if input is larger than or equal to 100). Maybe I should have checked some more test cases myself more carefully before posting.. Sorry about that..– Kevin Cruijssen
16 hours ago
4
4
By the way, loving the top hat on a rubix cube!
– AJFaraday
16 hours ago
By the way, loving the top hat on a rubix cube!
– AJFaraday
16 hours ago
|
show 2 more comments
up vote
4
down vote
JavaScript (ES6), 89 bytes
n=>(s='01111112111312222322',n>99&&+s[n/100|0]+3-!(n%=100))+~~(s[n]||+s[n/10|0]-~s[n%10])
Try it online!
add a comment |
up vote
4
down vote
JavaScript (ES6), 89 bytes
n=>(s='01111112111312222322',n>99&&+s[n/100|0]+3-!(n%=100))+~~(s[n]||+s[n/10|0]-~s[n%10])
Try it online!
add a comment |
up vote
4
down vote
up vote
4
down vote
JavaScript (ES6), 89 bytes
n=>(s='01111112111312222322',n>99&&+s[n/100|0]+3-!(n%=100))+~~(s[n]||+s[n/10|0]-~s[n%10])
Try it online!
JavaScript (ES6), 89 bytes
n=>(s='01111112111312222322',n>99&&+s[n/100|0]+3-!(n%=100))+~~(s[n]||+s[n/10|0]-~s[n%10])
Try it online!
edited 7 hours ago
answered 15 hours ago
Arnauld
71.6k688299
71.6k688299
add a comment |
add a comment |
up vote
2
down vote
Jelly, 28 25 23 bytes
9ḊŻ;2+⁵Żċ%ȷ2$ạDṠḄƊ+Dċ7Ɗ
Try it online!
How it works
9ḊŻ;2+⁵Żċ%ȷ2$ạDṠḄƊ+Dċ7Ɗ Main link. Argument: n (integer in [1, ..., 999])
9 Set the return value to 9.
Ḋ Dequeue; yield [2, 3, 4, 5, 6, 7, 8, 9].
Ż Zero; yield [0, 2, 3, 4, 5, 6, 7, 8, 9].
;2 Concat 2, yield [0, 2, 3, 4, 5, 6, 7, 8, 9, 2].
+⁵ Add 10; yield [10, 12, 13, 14, 15, 16, 17, 18, 19, 12].
Ż Zero; yield [0, 10, 12, 13, 14, 15, 16, 17, 18, 19, 12].
%ȷ2$ Yield n % 1e2.
ċ Count the occurrences of the modulus in the array.
Ɗ Combine the three links to the left into a monadic chain.
D Decimal; convert n to its array of digits in base 10.
Ṡ Take the sign of each decimal digit (0 or 1).
Ḅ Convert the array of signs from base 2 to integer.
ạ Compute the abs. difference of the results to both sides.
Ɗ Combine the three links to the left into a monadic chain.
D Decimal; convert n to its array of digits in base 10.
ċ7 Count the number of 7's.
add a comment |
up vote
2
down vote
Jelly, 28 25 23 bytes
9ḊŻ;2+⁵Żċ%ȷ2$ạDṠḄƊ+Dċ7Ɗ
Try it online!
How it works
9ḊŻ;2+⁵Żċ%ȷ2$ạDṠḄƊ+Dċ7Ɗ Main link. Argument: n (integer in [1, ..., 999])
9 Set the return value to 9.
Ḋ Dequeue; yield [2, 3, 4, 5, 6, 7, 8, 9].
Ż Zero; yield [0, 2, 3, 4, 5, 6, 7, 8, 9].
;2 Concat 2, yield [0, 2, 3, 4, 5, 6, 7, 8, 9, 2].
+⁵ Add 10; yield [10, 12, 13, 14, 15, 16, 17, 18, 19, 12].
Ż Zero; yield [0, 10, 12, 13, 14, 15, 16, 17, 18, 19, 12].
%ȷ2$ Yield n % 1e2.
ċ Count the occurrences of the modulus in the array.
Ɗ Combine the three links to the left into a monadic chain.
D Decimal; convert n to its array of digits in base 10.
Ṡ Take the sign of each decimal digit (0 or 1).
Ḅ Convert the array of signs from base 2 to integer.
ạ Compute the abs. difference of the results to both sides.
Ɗ Combine the three links to the left into a monadic chain.
D Decimal; convert n to its array of digits in base 10.
ċ7 Count the number of 7's.
add a comment |
up vote
2
down vote
up vote
2
down vote
Jelly, 28 25 23 bytes
9ḊŻ;2+⁵Żċ%ȷ2$ạDṠḄƊ+Dċ7Ɗ
Try it online!
How it works
9ḊŻ;2+⁵Żċ%ȷ2$ạDṠḄƊ+Dċ7Ɗ Main link. Argument: n (integer in [1, ..., 999])
9 Set the return value to 9.
Ḋ Dequeue; yield [2, 3, 4, 5, 6, 7, 8, 9].
Ż Zero; yield [0, 2, 3, 4, 5, 6, 7, 8, 9].
;2 Concat 2, yield [0, 2, 3, 4, 5, 6, 7, 8, 9, 2].
+⁵ Add 10; yield [10, 12, 13, 14, 15, 16, 17, 18, 19, 12].
Ż Zero; yield [0, 10, 12, 13, 14, 15, 16, 17, 18, 19, 12].
%ȷ2$ Yield n % 1e2.
ċ Count the occurrences of the modulus in the array.
Ɗ Combine the three links to the left into a monadic chain.
D Decimal; convert n to its array of digits in base 10.
Ṡ Take the sign of each decimal digit (0 or 1).
Ḅ Convert the array of signs from base 2 to integer.
ạ Compute the abs. difference of the results to both sides.
Ɗ Combine the three links to the left into a monadic chain.
D Decimal; convert n to its array of digits in base 10.
ċ7 Count the number of 7's.
Jelly, 28 25 23 bytes
9ḊŻ;2+⁵Żċ%ȷ2$ạDṠḄƊ+Dċ7Ɗ
Try it online!
How it works
9ḊŻ;2+⁵Żċ%ȷ2$ạDṠḄƊ+Dċ7Ɗ Main link. Argument: n (integer in [1, ..., 999])
9 Set the return value to 9.
Ḋ Dequeue; yield [2, 3, 4, 5, 6, 7, 8, 9].
Ż Zero; yield [0, 2, 3, 4, 5, 6, 7, 8, 9].
;2 Concat 2, yield [0, 2, 3, 4, 5, 6, 7, 8, 9, 2].
+⁵ Add 10; yield [10, 12, 13, 14, 15, 16, 17, 18, 19, 12].
Ż Zero; yield [0, 10, 12, 13, 14, 15, 16, 17, 18, 19, 12].
%ȷ2$ Yield n % 1e2.
ċ Count the occurrences of the modulus in the array.
Ɗ Combine the three links to the left into a monadic chain.
D Decimal; convert n to its array of digits in base 10.
Ṡ Take the sign of each decimal digit (0 or 1).
Ḅ Convert the array of signs from base 2 to integer.
ạ Compute the abs. difference of the results to both sides.
Ɗ Combine the three links to the left into a monadic chain.
D Decimal; convert n to its array of digits in base 10.
ċ7 Count the number of 7's.
edited 14 hours ago
answered 15 hours ago
Dennis♦
186k32295734
186k32295734
add a comment |
add a comment |
up vote
2
down vote
JavaScript, 86 84 bytes
A tweaked port of TFeld's Python solution.
f=n=>n>99?f(n/100)+3+f(n%=100)-!n:n>19?f(n/10)-~f(n%10):+"01111112111312222322"[n|0]
Try it online
2 bytes saved thanks to Arnauld
add a comment |
up vote
2
down vote
JavaScript, 86 84 bytes
A tweaked port of TFeld's Python solution.
f=n=>n>99?f(n/100)+3+f(n%=100)-!n:n>19?f(n/10)-~f(n%10):+"01111112111312222322"[n|0]
Try it online
2 bytes saved thanks to Arnauld
add a comment |
up vote
2
down vote
up vote
2
down vote
JavaScript, 86 84 bytes
A tweaked port of TFeld's Python solution.
f=n=>n>99?f(n/100)+3+f(n%=100)-!n:n>19?f(n/10)-~f(n%10):+"01111112111312222322"[n|0]
Try it online
2 bytes saved thanks to Arnauld
JavaScript, 86 84 bytes
A tweaked port of TFeld's Python solution.
f=n=>n>99?f(n/100)+3+f(n%=100)-!n:n>19?f(n/10)-~f(n%10):+"01111112111312222322"[n|0]
Try it online
2 bytes saved thanks to Arnauld
edited 12 hours ago
answered 12 hours ago
Shaggy
18.7k21663
18.7k21663
add a comment |
add a comment |
up vote
2
down vote
Java 11, 105 bytes
n->(""+"".repeat(8)).charAt(n%100)+(n+"").split("7",-1).length-6+(n>99?4:0)
Contains loads of unprintable characters.
Try it online.
Explanation:
n-> // Method with integer as both parameter and return-type
("" // Push string with ASCII-value digits 46666666666867777777
+"".repeat(8)) // Appended with 8 times a string with ASCII-value digits 7888888888
.charAt(n%100) // Take the (input modulo-100)'th character of this string (as integer)
+(n+"").split("7",-1).length
// Count the amount of 7s in the input + 1
-6 // Subtract 6 (1 for the 7s+1 count and 5 to map the ASCII-digits to:
// 4 → -1; 6 → 1; 7 → 2; 8 → 3)
+(n>99? // And if the input is larger than 99:
4 // Add 4
: // Else:
0) // Leave it the same by adding 0
Fyi, the TIO-generated PPCG answer would include the unprintables as well, which SE strips otherwise.
– Dennis♦
6 hours ago
add a comment |
up vote
2
down vote
Java 11, 105 bytes
n->(""+"".repeat(8)).charAt(n%100)+(n+"").split("7",-1).length-6+(n>99?4:0)
Contains loads of unprintable characters.
Try it online.
Explanation:
n-> // Method with integer as both parameter and return-type
("" // Push string with ASCII-value digits 46666666666867777777
+"".repeat(8)) // Appended with 8 times a string with ASCII-value digits 7888888888
.charAt(n%100) // Take the (input modulo-100)'th character of this string (as integer)
+(n+"").split("7",-1).length
// Count the amount of 7s in the input + 1
-6 // Subtract 6 (1 for the 7s+1 count and 5 to map the ASCII-digits to:
// 4 → -1; 6 → 1; 7 → 2; 8 → 3)
+(n>99? // And if the input is larger than 99:
4 // Add 4
: // Else:
0) // Leave it the same by adding 0
Fyi, the TIO-generated PPCG answer would include the unprintables as well, which SE strips otherwise.
– Dennis♦
6 hours ago
add a comment |
up vote
2
down vote
up vote
2
down vote
Java 11, 105 bytes
n->(""+"".repeat(8)).charAt(n%100)+(n+"").split("7",-1).length-6+(n>99?4:0)
Contains loads of unprintable characters.
Try it online.
Explanation:
n-> // Method with integer as both parameter and return-type
("" // Push string with ASCII-value digits 46666666666867777777
+"".repeat(8)) // Appended with 8 times a string with ASCII-value digits 7888888888
.charAt(n%100) // Take the (input modulo-100)'th character of this string (as integer)
+(n+"").split("7",-1).length
// Count the amount of 7s in the input + 1
-6 // Subtract 6 (1 for the 7s+1 count and 5 to map the ASCII-digits to:
// 4 → -1; 6 → 1; 7 → 2; 8 → 3)
+(n>99? // And if the input is larger than 99:
4 // Add 4
: // Else:
0) // Leave it the same by adding 0
Java 11, 105 bytes
n->(""+"".repeat(8)).charAt(n%100)+(n+"").split("7",-1).length-6+(n>99?4:0)
Contains loads of unprintable characters.
Try it online.
Explanation:
n-> // Method with integer as both parameter and return-type
("" // Push string with ASCII-value digits 46666666666867777777
+"".repeat(8)) // Appended with 8 times a string with ASCII-value digits 7888888888
.charAt(n%100) // Take the (input modulo-100)'th character of this string (as integer)
+(n+"").split("7",-1).length
// Count the amount of 7s in the input + 1
-6 // Subtract 6 (1 for the 7s+1 count and 5 to map the ASCII-digits to:
// 4 → -1; 6 → 1; 7 → 2; 8 → 3)
+(n>99? // And if the input is larger than 99:
4 // Add 4
: // Else:
0) // Leave it the same by adding 0
answered 8 hours ago
Kevin Cruijssen
35.4k554186
35.4k554186
Fyi, the TIO-generated PPCG answer would include the unprintables as well, which SE strips otherwise.
– Dennis♦
6 hours ago
add a comment |
Fyi, the TIO-generated PPCG answer would include the unprintables as well, which SE strips otherwise.
– Dennis♦
6 hours ago
Fyi, the TIO-generated PPCG answer would include the unprintables as well, which SE strips otherwise.
– Dennis♦
6 hours ago
Fyi, the TIO-generated PPCG answer would include the unprintables as well, which SE strips otherwise.
– Dennis♦
6 hours ago
add a comment |
up vote
0
down vote
Charcoal, 39 bytes
≔﹪N¹⁰⁰ηI⁻⁻⁻⁺⁺↨E謬Iι²№θ7⁼η¹¹⁼η¹²¬η⁼¹÷ηχ
Try it online! Link is to verbose version of code. This is an arithmetic solution. Explanation:
≔﹪N¹⁰⁰η
Take the input number modulo 100.
I⁻⁻⁻⁺⁺
Calculate adjustments to the number of syllables and output the result as a string.
↨E謬Iι²
Start by changing each non-zero digit to 1 and then decoding as base 2. This gives the correct answer for most inputs.
№θ7
Add 1 for each 7
.
⁼η¹¹
Add 1 if the number ends in 11
.
⁼η¹²
Subtract 1 if the number ends in 12
.
¬η
Subtract 1 if the number ends in 00
.
⁼¹÷ηχ
Subtract 1 if the number ends in 1X
(where X is any digit including 1
and 2
).
add a comment |
up vote
0
down vote
Charcoal, 39 bytes
≔﹪N¹⁰⁰ηI⁻⁻⁻⁺⁺↨E謬Iι²№θ7⁼η¹¹⁼η¹²¬η⁼¹÷ηχ
Try it online! Link is to verbose version of code. This is an arithmetic solution. Explanation:
≔﹪N¹⁰⁰η
Take the input number modulo 100.
I⁻⁻⁻⁺⁺
Calculate adjustments to the number of syllables and output the result as a string.
↨E謬Iι²
Start by changing each non-zero digit to 1 and then decoding as base 2. This gives the correct answer for most inputs.
№θ7
Add 1 for each 7
.
⁼η¹¹
Add 1 if the number ends in 11
.
⁼η¹²
Subtract 1 if the number ends in 12
.
¬η
Subtract 1 if the number ends in 00
.
⁼¹÷ηχ
Subtract 1 if the number ends in 1X
(where X is any digit including 1
and 2
).
add a comment |
up vote
0
down vote
up vote
0
down vote
Charcoal, 39 bytes
≔﹪N¹⁰⁰ηI⁻⁻⁻⁺⁺↨E謬Iι²№θ7⁼η¹¹⁼η¹²¬η⁼¹÷ηχ
Try it online! Link is to verbose version of code. This is an arithmetic solution. Explanation:
≔﹪N¹⁰⁰η
Take the input number modulo 100.
I⁻⁻⁻⁺⁺
Calculate adjustments to the number of syllables and output the result as a string.
↨E謬Iι²
Start by changing each non-zero digit to 1 and then decoding as base 2. This gives the correct answer for most inputs.
№θ7
Add 1 for each 7
.
⁼η¹¹
Add 1 if the number ends in 11
.
⁼η¹²
Subtract 1 if the number ends in 12
.
¬η
Subtract 1 if the number ends in 00
.
⁼¹÷ηχ
Subtract 1 if the number ends in 1X
(where X is any digit including 1
and 2
).
Charcoal, 39 bytes
≔﹪N¹⁰⁰ηI⁻⁻⁻⁺⁺↨E謬Iι²№θ7⁼η¹¹⁼η¹²¬η⁼¹÷ηχ
Try it online! Link is to verbose version of code. This is an arithmetic solution. Explanation:
≔﹪N¹⁰⁰η
Take the input number modulo 100.
I⁻⁻⁻⁺⁺
Calculate adjustments to the number of syllables and output the result as a string.
↨E謬Iι²
Start by changing each non-zero digit to 1 and then decoding as base 2. This gives the correct answer for most inputs.
№θ7
Add 1 for each 7
.
⁼η¹¹
Add 1 if the number ends in 11
.
⁼η¹²
Subtract 1 if the number ends in 12
.
¬η
Subtract 1 if the number ends in 00
.
⁼¹÷ηχ
Subtract 1 if the number ends in 1X
(where X is any digit including 1
and 2
).
answered 9 hours ago
Neil
78.9k744175
78.9k744175
add a comment |
add a comment |
up vote
0
down vote
Perl 5 -pF
, 53 bytes
$_=4*/.../+2*/[^0].$/+!/0$/+y/7//-/1[^1]$/-/12$/-/00/
Try it online!
How
$_=4*/.../ # Hundreds place has minimum of 4 sylables (__ HUN-DRED AND),
# match fails on number <100, and would add 0 here
+2*/[^0].$/ # Tens place has two syllables if not 0 (__-TY or __TEEN),
# match fails on numbers <10, and would add 0
+!/0$/ # Ones place has one syllable if not 0 (__)
# -- Now adjust for special cases --
+y/7// # add a syllable for every 7 present
-/1[^1]$/ # remove a syllable for 10-19, except 11
-/12$/ # remove another syllable for 12
-/00/ # remove the syllable for AND if it's an even hundred
add a comment |
up vote
0
down vote
Perl 5 -pF
, 53 bytes
$_=4*/.../+2*/[^0].$/+!/0$/+y/7//-/1[^1]$/-/12$/-/00/
Try it online!
How
$_=4*/.../ # Hundreds place has minimum of 4 sylables (__ HUN-DRED AND),
# match fails on number <100, and would add 0 here
+2*/[^0].$/ # Tens place has two syllables if not 0 (__-TY or __TEEN),
# match fails on numbers <10, and would add 0
+!/0$/ # Ones place has one syllable if not 0 (__)
# -- Now adjust for special cases --
+y/7// # add a syllable for every 7 present
-/1[^1]$/ # remove a syllable for 10-19, except 11
-/12$/ # remove another syllable for 12
-/00/ # remove the syllable for AND if it's an even hundred
add a comment |
up vote
0
down vote
up vote
0
down vote
Perl 5 -pF
, 53 bytes
$_=4*/.../+2*/[^0].$/+!/0$/+y/7//-/1[^1]$/-/12$/-/00/
Try it online!
How
$_=4*/.../ # Hundreds place has minimum of 4 sylables (__ HUN-DRED AND),
# match fails on number <100, and would add 0 here
+2*/[^0].$/ # Tens place has two syllables if not 0 (__-TY or __TEEN),
# match fails on numbers <10, and would add 0
+!/0$/ # Ones place has one syllable if not 0 (__)
# -- Now adjust for special cases --
+y/7// # add a syllable for every 7 present
-/1[^1]$/ # remove a syllable for 10-19, except 11
-/12$/ # remove another syllable for 12
-/00/ # remove the syllable for AND if it's an even hundred
Perl 5 -pF
, 53 bytes
$_=4*/.../+2*/[^0].$/+!/0$/+y/7//-/1[^1]$/-/12$/-/00/
Try it online!
How
$_=4*/.../ # Hundreds place has minimum of 4 sylables (__ HUN-DRED AND),
# match fails on number <100, and would add 0 here
+2*/[^0].$/ # Tens place has two syllables if not 0 (__-TY or __TEEN),
# match fails on numbers <10, and would add 0
+!/0$/ # Ones place has one syllable if not 0 (__)
# -- Now adjust for special cases --
+y/7// # add a syllable for every 7 present
-/1[^1]$/ # remove a syllable for 10-19, except 11
-/12$/ # remove another syllable for 12
-/00/ # remove the syllable for AND if it's an even hundred
edited 7 hours ago
answered 9 hours ago
Xcali
5,067520
5,067520
add a comment |
add a comment |
up vote
0
down vote
PHP, 158 bytes
<?php $i=fgets(STDIN);for($j=$p=0;$p<strlen($i);){!str_split($i)[$p++]>0?:++$j;};echo substr_count($i,7)+$j+3*($i>=100)-!($i%=100)+($i>=20)-($i==12)+($i==11);
Try it online!
A port of Kevin Cruijssen's solution (unfortunately it doesn't have the same brevity in PHP :) )
-32 thanks to Shaggy!
So many savings to be made here! Here's just a few very quick ones
– Shaggy
5 hours ago
add a comment |
up vote
0
down vote
PHP, 158 bytes
<?php $i=fgets(STDIN);for($j=$p=0;$p<strlen($i);){!str_split($i)[$p++]>0?:++$j;};echo substr_count($i,7)+$j+3*($i>=100)-!($i%=100)+($i>=20)-($i==12)+($i==11);
Try it online!
A port of Kevin Cruijssen's solution (unfortunately it doesn't have the same brevity in PHP :) )
-32 thanks to Shaggy!
So many savings to be made here! Here's just a few very quick ones
– Shaggy
5 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
PHP, 158 bytes
<?php $i=fgets(STDIN);for($j=$p=0;$p<strlen($i);){!str_split($i)[$p++]>0?:++$j;};echo substr_count($i,7)+$j+3*($i>=100)-!($i%=100)+($i>=20)-($i==12)+($i==11);
Try it online!
A port of Kevin Cruijssen's solution (unfortunately it doesn't have the same brevity in PHP :) )
-32 thanks to Shaggy!
PHP, 158 bytes
<?php $i=fgets(STDIN);for($j=$p=0;$p<strlen($i);){!str_split($i)[$p++]>0?:++$j;};echo substr_count($i,7)+$j+3*($i>=100)-!($i%=100)+($i>=20)-($i==12)+($i==11);
Try it online!
A port of Kevin Cruijssen's solution (unfortunately it doesn't have the same brevity in PHP :) )
-32 thanks to Shaggy!
edited 5 hours ago
answered 5 hours ago
NK1406
449213
449213
So many savings to be made here! Here's just a few very quick ones
– Shaggy
5 hours ago
add a comment |
So many savings to be made here! Here's just a few very quick ones
– Shaggy
5 hours ago
So many savings to be made here! Here's just a few very quick ones
– Shaggy
5 hours ago
So many savings to be made here! Here's just a few very quick ones
– Shaggy
5 hours ago
add a comment |
up vote
0
down vote
Wolfram Language 101 Bytes
s = StringSplit;Length[Join@@(WordData[#,"Hyphenation"]&/@
Join@@s/@s[IntegerName@#,"-"])]+Boole[#>100]&
Explanation
(substituting StringSplit
for s
)
Length[Join@@(WordData[#,"Hyphenation"]&/@
Join@@StringSplit/@StringSplit[IntegerName@#,"-"])]+Boole[#>100]&
IntegerName
renders the number in American English (i.e. without "and" included in numbers greater than 100.) E.g. 777-> "seven hundred seventy-seven
.
StringSplit[IntegerName@#,"-"]
removes any hyphens in the rendering.
StringSplit/@
splits the rendering into words.
Join@@
leaves a simple list of words, without embedded list (in the case that a hyphen appeared).
WordData[#,"Hyphenation"]
breaks up a single word into its syllables.
Join@@
leaves a simple list of syllables in all of the words.
Length
counts the syllables
+Boole[#>100]
adds 1
to the syllable count for those numbers greater than 100 (because of the additional "and" employed in the British English rendering).
add a comment |
up vote
0
down vote
Wolfram Language 101 Bytes
s = StringSplit;Length[Join@@(WordData[#,"Hyphenation"]&/@
Join@@s/@s[IntegerName@#,"-"])]+Boole[#>100]&
Explanation
(substituting StringSplit
for s
)
Length[Join@@(WordData[#,"Hyphenation"]&/@
Join@@StringSplit/@StringSplit[IntegerName@#,"-"])]+Boole[#>100]&
IntegerName
renders the number in American English (i.e. without "and" included in numbers greater than 100.) E.g. 777-> "seven hundred seventy-seven
.
StringSplit[IntegerName@#,"-"]
removes any hyphens in the rendering.
StringSplit/@
splits the rendering into words.
Join@@
leaves a simple list of words, without embedded list (in the case that a hyphen appeared).
WordData[#,"Hyphenation"]
breaks up a single word into its syllables.
Join@@
leaves a simple list of syllables in all of the words.
Length
counts the syllables
+Boole[#>100]
adds 1
to the syllable count for those numbers greater than 100 (because of the additional "and" employed in the British English rendering).
add a comment |
up vote
0
down vote
up vote
0
down vote
Wolfram Language 101 Bytes
s = StringSplit;Length[Join@@(WordData[#,"Hyphenation"]&/@
Join@@s/@s[IntegerName@#,"-"])]+Boole[#>100]&
Explanation
(substituting StringSplit
for s
)
Length[Join@@(WordData[#,"Hyphenation"]&/@
Join@@StringSplit/@StringSplit[IntegerName@#,"-"])]+Boole[#>100]&
IntegerName
renders the number in American English (i.e. without "and" included in numbers greater than 100.) E.g. 777-> "seven hundred seventy-seven
.
StringSplit[IntegerName@#,"-"]
removes any hyphens in the rendering.
StringSplit/@
splits the rendering into words.
Join@@
leaves a simple list of words, without embedded list (in the case that a hyphen appeared).
WordData[#,"Hyphenation"]
breaks up a single word into its syllables.
Join@@
leaves a simple list of syllables in all of the words.
Length
counts the syllables
+Boole[#>100]
adds 1
to the syllable count for those numbers greater than 100 (because of the additional "and" employed in the British English rendering).
Wolfram Language 101 Bytes
s = StringSplit;Length[Join@@(WordData[#,"Hyphenation"]&/@
Join@@s/@s[IntegerName@#,"-"])]+Boole[#>100]&
Explanation
(substituting StringSplit
for s
)
Length[Join@@(WordData[#,"Hyphenation"]&/@
Join@@StringSplit/@StringSplit[IntegerName@#,"-"])]+Boole[#>100]&
IntegerName
renders the number in American English (i.e. without "and" included in numbers greater than 100.) E.g. 777-> "seven hundred seventy-seven
.
StringSplit[IntegerName@#,"-"]
removes any hyphens in the rendering.
StringSplit/@
splits the rendering into words.
Join@@
leaves a simple list of words, without embedded list (in the case that a hyphen appeared).
WordData[#,"Hyphenation"]
breaks up a single word into its syllables.
Join@@
leaves a simple list of syllables in all of the words.
Length
counts the syllables
+Boole[#>100]
adds 1
to the syllable count for those numbers greater than 100 (because of the additional "and" employed in the British English rendering).
answered 4 hours ago
DavidC
23.7k243102
23.7k243102
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f177586%2fhow-many-syllables-in-that-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can we take input as a string or an array of digits?
– Dennis♦
14 hours ago