| Bytes | Lang | Time | Link |
|---|---|---|---|
| 072 | Haskell + hgl | 240717T144836Z | Wheat Wi |
| 165 | Lexurgy | 240717T161918Z | bigyihsu |
| 060 | JavaScript Node.js | 181017T093417Z | tsh |
| 060 | Perl 6 | 181017T103044Z | nwellnho |
| 688 | Retina 0.8.2 | 181017T082607Z | Neil |
| 032 | Jelly | 181016T225107Z | Jonathan |
| nan | APL Dyalog | 181016T234035Z | Adalynn |
Haskell + hgl, 72 bytes
Uses u and d to indicate tones
f=fcq sA<fmX ao<K
skX"du_|d_.+u"<<fo<<f"d"^.f"u"1<gky(rX".[ャュョ]?")
Explanation
We define f as a function to insert a value at a particular location in a list. We will use this to insert the tone markings.
gky(rX".[ャュョ]?"): break the input into moras using a regexf"u"1: insert a"u"between the first and second morasf"d": insert a "d" before the syllable given by the indexfo: concat back togetherskX"du_|d_.+u": remove audirectly following ador adthat comes before aupossibly with intermediate characters.
No regex, 81 bytes
Uses / and , to indicate tones
f=fcq sA<fmX ao<K
fo<<(!)(f"/"1:f","1:f"/"1<<f","<eF2)^.gky(hdS<>?xys"ャュョ")
Explanation
We use the same f function as the last answer.
gky(rX".[ャュョ]?")breaks the input into the moras.f"/"1:f","1:f"/"1<<f","<P1<nNis an infinite list of functions with the function at the nth place being the function to insert the tones for the nth pitch accent.(!)indexes our list of functions by the pitch accent number.focombines the pieces back together.
Reflection
This is very long, I think it could be shorter somehow, but I'm not sure. There are a couple of improvements that already jump out though.
- I don't know why
(!)still doesn't have a prefix, this is getting annoying. - The
fwe've defined here should just be a library function. - There should be a shorthand for
ʃ<p/p<<χ. It wouldn't improve either the regex or regex free version but I noticed it didn't exist. - I could add a function for
(*:*)^.pYwhich save one byte on the no regex version, but I am not convinced of the overall utility of this function. - It would be useful to have a function which maps across the tail of the list. This would make it cheaper to insert things for this particular challenge and seems like it would generally be useful.
Lexurgy, 442 165 bytes
class F {I,ャ,ュ,ョ,^,v}
element s !@F @F*(0-2)
h:
*=>^/$ @s _
I @s$1 *=>* $1 v/$ _
@s$1 * *=>v $1 ^/$ I+ _
l propagate:
I=>*/_ !@F
then:
v @s$1 => $1 v/$ I []* _
Expects input of the form IK, where I is the unary representation of the kind of pitch accent with each digit being I, and K which is the kana string. So, the input 4 アタラシイ would be IIIIアタラシイ.
Output uses ^v for /\.
- -227 bytes by removing the big list of kana. The core functionality is the same, though.
Ungolfed Explanation
class K {ア,イ,ウ,エ,オ,カ,キ,ク,ケ,コ,サ,シ,ス,セ,ソ,タ,チ,ツ,テ,ト,ナ,ニ,ヌ,ネ,ノ,ハ,ヒ,フ,ヘ,ホ,マ,ミ,ム,メ,モ,ヤ,ユ,ヨ,ラ,リ,ル,レ,ロ,ワ,ン,ッ,ガ,ギ,グ,ゲ,ゴ,ザ,ジ,ズ,ゼ,ゾ,ダ,ヂ,ヅ,デ,ド,バ,ビ,ブ,ベ,ボ,パ,ピ,プ,ペ,ポ}
class Y {ャ,ュ,ョ}
class p {^,v}
element syl @K @Y? @p? # a mora is a big kana with optional small kana. add ^,v so that it's considered part of the syllable
# `syllable` wasn't used here because it ended up being really hard to implement using syllables, and trying to account for the unary numeral at the front.
# use unary I for counting
# no I for 0, one I for 1, etc.
heibangata:
# 0 = LHH...
* => ^ / $ @syl _
atamadakagata:
# 1 = HLL...
I @syl$1 * => * $1 v / $ _
the-rest-start:
# k>1= L.k*H.L...
# start by placing a rise after the 1st syllable:
@syl$1 * * => v $1 ^ / $ I+ _
# then propagate so that we delete one I each time, and move the L to the right 1 syllable
the-rest-loop propagate:
I => * / _ @K # delete an I
then:
v @syl$1 => $1 v / $ I []* _ # move the v to the right when there is an I to start
JavaScript (Node.js), 60 bytes
n=>s=>s.replace(/.[ャュョ]?/g,s=>i=--n-i?s+1:n?s:s+2,i=0)
Perl 6, 63 60 bytes
-3 bytes thanks to Lynn's suggestion to use other separators than / \
->\n{&{S:g[.<[ャュョ]>?]=$/~7 x!$++*?+^-n~1 x(++$==n>0)}}
Curried function. Uses 7 1 instead of / \.
Explanation
->\n{ # Block taking argument n
&{ # Return a block
S:g # Replace globally
[.<[ャュョ]>?] # regex for mora
=$/ # with match
~7 # followed by 7
x!$++*?+^-n # if i==1 and n!=1
# should be read as (! $++) * (? +^ - n)
# +^ is bitwise negation like ~ in C
# ? converts to Bool like !! in C
# so ?+^-n is like !!~-n or !!(n-1) or n!=1
~1 # followed by 1
x(++$==n>0) # if i==n and n>0
}
}
Retina 0.8.2, 68 bytes (UTF-8)
\d+(.[ャュョ]?)
$1/$&$*1\
/1\b
/.
/
+`1(1*.)(.[ャュョ]?)
$2$1
Try it online! Link includes test cases. Explanation:
\d+(.[ャュョ]?)
$1/$&$*1\
Convert the number to unary, wrap it in /\, and insert it after the first mora.
/1\b
If the number was 1, then delete it and the /, leaving the \.
/.
/
Subtract 1 from the number, but in the case of 0, this deletes the \, leaving the /.
+`1(1*.)(.[ャュョ]?)
$2$1
Move the \ one mora to the right for each remaining 1.
Jelly, 32 bytes
O%62ḟ€“579‘œṗ⁸Ḋ⁹‘+Ị$,2¤œṖżCØ.ḟƊ}
A full program accepting a list of characters and a non-negative integer which prints using 0 for / and 1 for \.
Try it online! Or see the test-suite.
How?
O%62ḟ€“579‘œṗ⁸Ḋ⁹‘+Ị$,2¤œṖżCØ.ḟƊ} - Main Link: list of characters S, integer N
O - convert to ordinals
%62 - modulo by 62
“579‘ - code-page indices list = [53,55,57]
- (representing the three optional characters)
€ - for each ordinal:
ḟ - filter discard if in [53,55,57]
⁸ - chain's left argument, S
œṗ - partition before truthy indices
- (so before the non optional character positions)
Ḋ - dequeue (remove the leading empty list)
œṖ - partition at indices...
¤ - nilad followed by (links) as a nilad:
⁹ - chain's right argument, N
‘ - increment
$ - last two links as a monad:
+ - add
Ị - insignificant? (1 if 1; 0 if 2+)
,2 - pair with 2
} - use right argument, N, for...
Ɗ - last three links as a monad:
C - compliment = 1-N
ḟ - filter discard if in...
Ø. - bits = [0,1]
- (i.e. if N=0:[0], if N=1:[1], otherwise:[0,1])
ż - zip together
- e.g. [[["ジュ"],0],[["ウ","イ","チ","ガ","ツ"],1]]
- implicit (smashing) print
- e.g. ジュ0ウイチガツ1
APL (Dyalog), 45 characters / 51 bytes* / 83 bytes
{S←⍵⊂⍨~⍵∊'ャュョ'⋄S[⍺~0],←'\'⋄S[0~⍨⍺≠1],←'/'⋄∊S}
The second score only applies if the three Katakana characters in the code can be counted as a different encoding than the other portion of the code.