Context
As a conlanger, I am interested in creating a uniform, naturalistic language. One of the tricks is to create vocabulary according to certain structures of words. An example from English: In English, we have the word "tap" structured consonant-vowel-consonant. Usually, this means that there are many other words of this structure: "cat", "dog", "rock", "fog", "good", etc.
Task
As input, you have:
- an array
C
containing strings: consonants (C
is the first letter in the word consonants). Identical consonants cannot be repeated on this list. For example, this list cannot contain ['b', 'b']. - an array
V
containing strings: vowels (V
is the first letter of the word vowels). Identical vowels cannot be repeated on this list. - string
S
, which contains something like this "CVCCV" (any combination of "C" and "V")
Your task is to replace "C" in the string with a randomly taken string from array C
and replace "V" in the string with a randomly taken string from array V
and return (or display) this string.
"randomly" is defined as all possibilities having an equal chance of being selected. This is a kind of simplification: in real languages as well as in conlangs, there are very frequent (for example 'r' in English) and not very frequent sounds but this is just a code-golf.
Rules
This is code-golf so the lowest byte count wins.
Examples
Input:
C = ['p', 'b', 't', 'd', 'k', 'g', 'm', 'n', 'w']
V = ['a', 'o', 'u', 'i', 'e', 'ä']
S = 'CVCCV'
Output:
pakto
Input:
C = ['p', 'b', 't', 'd', 'k', 'g', 'v', 's', 'r']
V = ['a', 'o', 'u', 'i', 'e', 'ä', 'ᵫ']
S = 'CVVCCVCCV'
Output:
koebrᵫvtä
12 Answers 12
Jelly, (削除) 5 (削除ここまで) 4 bytes
-1 thanks to Neil!
OịX€
A dyadic Link accepting a list of characters, S
, on the left and a list of two lists of characters, [C, V]
, on the right which yields a list of characters.
If we could take S
as a list of 1
s and 0
s we'd have the three byte solution ịX€
.
How?
OịX€ - Link: S, [C,V]
O - ordinals (of S) i.e. 'C':67 'V':86
ị - index into [C,V] (vectorises) C V [Jelly indexing is 1-indexed and modular]
€ - for each:
X - random choice
-
\$\begingroup\$ I like both solutions! The 5 and the 3 byte ones +1 \$\endgroup\$RGS– RGS2020年02月22日 16:54:15 +00:00Commented Feb 22, 2020 at 16:54
-
\$\begingroup\$ What does Jelly do if an index is out of range? (Charcoal automatically reduces it modulo the range, so I don't need to extract the bit manually.) \$\endgroup\$Neil– Neil2020年02月22日 17:42:40 +00:00Commented Feb 22, 2020 at 17:42
-
\$\begingroup\$ @Neil you just saved a byte - thanks! I even noted in my code break-down that "Jelly indexing is 1-indexed and modular". \$\endgroup\$Jonathan Allan– Jonathan Allan2020年02月22日 17:47:56 +00:00Commented Feb 22, 2020 at 17:47
Python 3, (削除) 92 (削除ここまで) (削除) 68 (削除ここまで) 61 bytes
Takes input as S
, C
, V
:
lambda s,*w:[random.choice(w[a>"u"])for a in s]
import random
You can try it online! How it works:
The *w
is used so that the lists of consonants and vowels are packed in a list w
.
When we do w[a > "u"]
we are essentially checking if a
(a character of s
) is "v" or not. If it is, then a > "u"
returns True
, which indexes as 1
into w
. If a
is "c", then a > "u"
returns False
, which indexes as 0
.
We use those indices to retrieve the correct list of letters from w
and then choose randomly from that list, picking the list of choices.
Thanks @Arnauld for saving one byte and to @Jonathan Allan for citing this meta, saving me 7 bytes.
-
\$\begingroup\$ This is what I would've done in my C solution had it not been for that the lists of consonants and vowels are not of equal length. \$\endgroup\$S.S. Anne– S.S. Anne2020年02月22日 17:57:04 +00:00Commented Feb 22, 2020 at 17:57
-
\$\begingroup\$ I believe that you should be able to take
s
as a list of characters and return the same (see this meta) and as such can replace the"".join(...)
with[...]
. \$\endgroup\$Jonathan Allan– Jonathan Allan2020年02月22日 19:48:40 +00:00Commented Feb 22, 2020 at 19:48 -
\$\begingroup\$ @JonathanAllan thanks for this reference! Shaved 7 bytes :) \$\endgroup\$RGS– RGS2020年02月22日 20:51:09 +00:00Commented Feb 22, 2020 at 20:51
APL+WIN, (削除) 35 (削除ここまで) 34 bytes
One byte saved thanks to Adam
Prompts for consonants and vowels as continuous strings and the desired output as a comma separated string
c←⍞⋄v←⍞⋄C←',c[?⍴c]'⋄V←',v[?⍴v]'⋄⍎⎕
-
\$\begingroup\$ If you liked this golf question, please vote to reopen it. \$\endgroup\$USERNAME GOES HERE– USERNAME GOES HERE2020年02月23日 14:01:48 +00:00Commented Feb 23, 2020 at 14:01
-
\$\begingroup\$ If you don't like this golf challenge, please retain its closed state. \$\endgroup\$user92069– user920692020年02月23日 14:23:39 +00:00Commented Feb 23, 2020 at 14:23
-
\$\begingroup\$ This is the first time I have been lobbied from both sides of a question closure. Personally I had no problem understanding the question and as I got two up votes I assume at least two people agreed with my interpretation and my answer \$\endgroup\$Graham– Graham2020年02月23日 15:04:50 +00:00Commented Feb 23, 2020 at 15:04
-
\$\begingroup\$ By using
⍞
instead of⎕
you can specify that input shouldn't have quotes, and then use⎕
instead of⍎⎕
to save a byte: Try it online! \$\endgroup\$Adám– Adám2020年02月25日 13:48:12 +00:00Commented Feb 25, 2020 at 13:48 -
\$\begingroup\$ @Adám Thanks for the suggestion but whilst it works in Dyalog Classic I cannot get it to work in APL+WIN. If I run the code as you suggest it simply outputs the concatenated code string which needs the second ⍎ to execute. \$\endgroup\$Graham– Graham2020年02月25日 16:28:17 +00:00Commented Feb 25, 2020 at 16:28
-
\$\begingroup\$ I didn't think that ruby could outgolf python. Great! \$\endgroup\$USERNAME GOES HERE– USERNAME GOES HERE2020年02月22日 16:59:15 +00:00Commented Feb 22, 2020 at 16:59
-
1\$\begingroup\$ Ruby code is usually shorter than the equivalent python code. \$\endgroup\$G B– G B2020年02月23日 07:14:23 +00:00Commented Feb 23, 2020 at 7:14
Charcoal, 7 bytes
⭆η‽§θc/oι
Try it online! Link is to verbose version of code. Takes the input in the form [V, C], S
where each value can be either a string or an array of characters as desired. Explanation:
η `S`
⭆ Map over elements/characters
θ `[V, C]`
§ Cyclically indexed by
c/o Ordinal of
ι Current element/character
‽ Random element/character
Implicitly print
C# (Visual C# Interactive Compiler), 66 bytes
(C,V,s)=>s.Select(x=>(x<68?C:V).OrderBy(l=>Guid.NewGuid()).Last())
-
\$\begingroup\$ @a'_' Ok.6 more to go... \$\endgroup\$USERNAME GOES HERE– USERNAME GOES HERE2020年02月23日 14:28:06 +00:00Commented Feb 23, 2020 at 14:28
-
\$\begingroup\$ @a'_' By the way, did you like this code-golf question? \$\endgroup\$USERNAME GOES HERE– USERNAME GOES HERE2020年02月23日 14:29:15 +00:00Commented Feb 23, 2020 at 14:29
-
\$\begingroup\$ @VictorVosMottorthanksMonica I loved it. But it was blatant that I can't re-open it, because I don't have enough reputation to do so. \$\endgroup\$user92069– user920692020年02月23日 14:30:37 +00:00Commented Feb 23, 2020 at 14:30
-
2\$\begingroup\$ @VictorVosMottorthanksMonica, liking a challenge is not a valid reason to VTR it. Address the issues that led to it being closed and, once done so to the community's satisfaction, it will be reopened. \$\endgroup\$Shaggy– Shaggy2020年02月23日 19:21:22 +00:00Commented Feb 23, 2020 at 19:21
-
3\$\begingroup\$ @a'_', similarly, not liking a challenge is not grounds for closing it or leaving it closed. There have been many challenges here over the years I haven't personally liked, as there have been for everyone - if you don't like a challenge, just skip it and move on to the next one. \$\endgroup\$Shaggy– Shaggy2020年02月23日 19:26:03 +00:00Commented Feb 23, 2020 at 19:26
W f
j
, 4 bytes
Hmm, let's do a pure-ASCII port of that.
C[gr
Explanation
C % Convert the string to its codepoints
[ % Index into the other list
r % For every indexed item:
g % "g"et a random item in this list
Flag:f % Flatten the output list
Flag:j % Join the flattened output list
```
-
1\$\begingroup\$ @KevinCruijssen I absolutely can't understand the challenge, so feel free to post your answer so that I can get a better understanding of this. (I guess I'd keep it deleted because I don't know how to fix it.) \$\endgroup\$user92069– user920692020年02月27日 11:35:48 +00:00Commented Feb 27, 2020 at 11:35
C (gcc), 92 bytes
f(s,c,v,m,n)char*s,**c,**v;{for(srand(&s);*s;)printf("%s",*s++/86?v[rand()%n]:c[rand()%m]);}
Takes as input a string (s
), an array of consonant strings (c
), an array of vowel strings v
, the length of c
(m
), and the length of v
(n
).
Could use a temporary variable for the result of rand
but it wouldn't save me anything.
JavaScript (ES6), (削除) 65 (削除ここまで) 64 bytes
(C,V,s)=>s.replace(/./g,x=>eval(x).sort(_=>Math.random()-.5)[0])
Or 55 bytes if we can use an array of characters as I/O.
-
\$\begingroup\$ The eval trick is really nice! Sadly enough I don't think I can use it in my answer to save some bytes :( \$\endgroup\$RGS– RGS2020年02月22日 16:49:05 +00:00Commented Feb 22, 2020 at 16:49
-
\$\begingroup\$ 55 seems fine unless OP has explicitly said not since I==O and if it quacks like a duck, it is a duck (i.e. strings are lists of characters) \$\endgroup\$Jonathan Allan– Jonathan Allan2020年02月22日 19:46:23 +00:00Commented Feb 22, 2020 at 19:46
-
\$\begingroup\$ @a'_' I think I'm privileged enough to hammer this question, which I think would be inappropriate either way. \$\endgroup\$Neil– Neil2020年02月24日 10:19:49 +00:00Commented Feb 24, 2020 at 10:19