| Bytes | Lang | Time | Link |
|---|---|---|---|
| 036 | Perl 5 + p | 240816T140242Z | Dom Hast |
| 076 | Scala 3 | 240818T123649Z | user |
| 145 | Scala 3 | 240818T025908Z | 138 Aspe |
| 145 | TypeScript's type system | 240818T001506Z | noodle p |
| 038 | Uiua | 240817T185422Z | janMakos |
| 018 | Jelly | 160607T045206Z | Dennis |
| 078 | Python 2 | 160607T055840Z | Dennis |
| 058 | Ruby | 160607T202922Z | Value In |
| 071 | Python 3 | 160607T195707Z | Dennis |
| 081 | Python | 160607T044707Z | Karl Nap |
| 1816 | Pyth | 160607T131150Z | FryAmThe |
| 070 | JavaScript ES6 | 160607T042440Z | Downgoat |
| 019 | CJam | 160607T055035Z | Martin E |
| 036 | Retina | 160607T081647Z | Martin E |
| 068 | Julia | 160607T053517Z | Dennis |
| 046 | Vim | 160607T051144Z | DJMcMayh |
| 019 | Pyth | 160607T042524Z | Leaky Nu |
Perl 5 + -p, 36 bytes
%a=<>=~/(\S)(\S*)/g;s/./$&$a{lc$&}/g
Explanation
The hash %a is built using the first letter of each segment (from <>, STDIN) as the key and any remaining letters (0 or more) as the value. Then s///ubstitution is performed on $_ replacing each character in the string with itself ($&) followed by the value from the hash at (the lowercase) key in %a. $_ is then implicitly -printed.
Scala 3, 76 bytes
w=>_.map(c=>c+(w split " "find(_(0)==c.toLower)getOrElse "a").tail).mkString
Based on this answer by 138 Aspen. Might be further golfable but I need to run now
Scala 3, 145 bytes
Golfed version. Attempt This Online!
(w,s)=>s.map(c=>(w+" "+w.split(" ").map(_.capitalize).mkString(" ")).split(" ").map(x=>x(0).toInt->x).toMap.getOrElse(c.toInt,c+"")).mkString("")
Ungolfed version. Attempt This Online!
object Main {
def f(w: String, s: String): String = {
// Create a translation map
val translationMap = (w + " " + w.split(" ").map(_.capitalize).mkString(" "))
.split(" ")
.map(word => word.head.toInt -> word)
.toMap
// Translate the string s using the created map
val translatedString = s.map(c => translationMap.getOrElse(c.toInt, c.toString)).mkString("")
translatedString
}
def main(args: Array[String]): Unit = {
val result = f("abra cadabra", (32 to 126).map(_.toChar).mkString)
println(result)
}
}
TypeScript's type system, 145 bytes
//@ts-ignore
type F<I,S,O="">=S extends`${infer C}${infer R}`?F<I,R,`${O}${C}${` ${I} `extends`${any} ${Lowercase<C>}${infer Z} ${any}`?Z:""}`>:O
Try it online: TypeScript Playground
This is a recursive generic type F<I, S> where I is a string-type representing the space-separated segments string, and S is the other string type. The //@ts-ignore comment is used to dismiss compiler errors.
Explanation:
The idea is to recursively loop through S, appending each character to a string O which begins empty and fills up with the final result.
Loop over the string S until it is empty by pattern-matching it against `${infer C}${infer R}` (which makes the first character C and the rest R) and recursing with F<I,R, ...> (keeping I the same and using R for the string). If the pattern-match fails because we have exhausted S, return the type parameter O which contains the result.
For the replacing logic, we attempt to pattern match ` ${I} ` (I surrounded by a space on both ends) against `${any} ${Lowercase<C>}${infer Z} ${any}` (which says match anything, a space, C converted to lowercase, some characters which are assigned to Z, another space, and anything). This effectively finds the suffix Z of the segment which starts with C. Then, Z—or nothing, if the match failed—is then appended to C and O.
Uiua, 38 bytes
/◇⊂⨬⋅∘(⊡⊙◌)⊸⊃∈(⊗:)⊙:⊙¤⊸≡◇⊢⊂⊸⍚⍜⊢⌵⊜□⊸≠@
This can probably be golfed more, but it's proving tough.
Explanation
⊜□⊸≠@ # split at spaces
⊂⊸⍚⍜⊢⌵ # join capitalized version
⊸≡◇⊢ # get all first letters
⊸⊃∈(⊗:)⊙:⊙¤ # make mask of substitution positions a
# and indices of the replacements
⨬⋅∘(⊡⊙◌) # substitute character with replacement
/◇⊂ # join into final string
Jelly, 18 bytes
ṣ⁶;Œu1¦€$;©ZḢiЀị®
Alternative version, 15 bytes
Jelly's title case function had a bug; it didn't capitalize the first word. That has been fixed, so the following works now.
ṣ⁶;Œt$;©ZḢiЀị®
This code does the same as in the competing version, except that Œt (title case) replaces the conditional uppercasing achieved by Œu1¦€.
How it works
ṣ⁶;Œu1¦€$;©ZḢiЀị® Main link. Left argument: w (words). Right argument: s (string)
ṣ⁶ Split w at spaces.
$ Combine the two links to the left into a monadic chain.
€ Map over the words.
Œu1¦ Uppercase the item at index 1.
; Append the result to the unmodified words.
; Append all characters in s to the list of words.
© Copy the result to the register.
Z Zip/transpose, grouping the first chars into the first list.
Ḣ Head; extract the list of first characters.
iЀ Find the first index of each character in s.
ị® Select the characters/strings from the list in the register
that are at those indices.
Python 2, 83 78 bytes
lambda w,s:''.join(c+w[(' '+w).find(' '+c.lower()):].split()[0][1:]for c in s)
Test it on Ideone.
How it works
We iterate over all characters c in the string s.
We prepend a space to the string of words w, then search for a occurrence of lowercased c, preceded by a space.
If such an occurrence exists,
findwill return the index of the space in the string' '+w, which matches the index of c in w.w[...:]thus returns the tail of w, starting from the word with first letter c.split()splits the tail at spaces,[0]selects the first chunk (the word) and[1:]removes its first letter.After prepending c to the previous result, we obtain the correctly cased word that starts with c.
If no word begins with c,
findwill return -1.Thus,
w[...:]yields the last character of w,split()wraps it in an array,[0]undoes the wrapping, and[1:]removes the only character from the string.After prepending c, we obtain the singleton string whose character is c, so the whole operation is a no-op.
Finally, ''.join concatenates all resulting strings, returning the Geobitsized version of s.
Python 3, 71 bytes
lambda w,s:s.translate({ord(t[0]):t for t in(w+' '+w.title()).split()})
Test it on Ideone.
How it works
In Python 3, the built-in str.translate takes a string and a dictionary, and replaces each character in the string whose code point is a key of that dictionary with the corresponding value, which may be a string, an integer or None (equivalent to the empty string).
Converting the string of words w to title case (i.e., capitalizing the first letter of each word) and appending it to the result of w+' ' creates a string of space separated words with lower- and uppercase version (first letter). Without a second argument, str.split splits at whitespace, so (w+' '+w.title()).split() creates the list of all words.
Finally, the dictionary comprehension {ord(t[0]):t for t in...} turns each word t into a dictionary entry with key ord(t[0]) (code point of the first letter) and value t, so str.translate will perform the intended substitutions.
Python, 126 99 95 81 bytes
Alot thanks to Dennis:
lambda G,S,j=''.join:j(s+j(g[1:]for g in G.split()if g[0]==s.lower())for s in S)
Edit1: don't need to append to a temporary
Edit2: S may contain uppercase...
Edit3: don't duplicate G
Edit4: compressed a little bit more and shoved it into one line
Edit5: using unnamed lambda and j=join' '
Pyth, 18 16
MsXGhMJcjdrBH3)J
Defines a function g that performs the geobitsising. As a program this would be a bit shorter if the second string is single line, but multiline input isn't worth it:
sXwhMJcjdrBz3)J
The general idea here was to title case the geobitsian string and append that to the original string. Then split that on spaces and for each string, take the first letter and map it to the string it represents. That way X will turn the first letter of each word into the full word.
JavaScript ES6, 67 63 70 bytes
g=>s=>s.replace(/\S/g,l=>l+(g.match(`\\b\\${l}(\\S+)`,'i')||[,""])[1])
Test this on Firefox. bugs are making this longer than I'd like
Explanation
function(gbarg, str) {
return str.replace(/\S/g, function(chr) { // Replace non-whitespace w/...
return chr + (
gbarg.match(`\\b\\${l}(\\S+)`,'i') // That word in the gbstr
||[,""])[1] // if not in gbstr, use blank str
});
}
CJam, 19 bytes
lq\S/_32af.^+_:c\er
Explanation
l e# Read first line of input (list of words).
q\ e# Read remaining input and swap with first line.
S/ e# Split around spaces.
_ e# Duplicate.
32af.^ e# Convert the first letter of each word to upper case by taking
e# the element-wise XOR with the list [32].
+ e# Append the upper-cased words to the original ones.
_:c e# Duplicate and convert each word to its first character.
\ e# Swap characters with words.
er e# Transliteration, replacing each character with the corresponding word.
Julia, 72 68 bytes
w%s=prod(c->"$c"join((W=w|>split)[find(t->t[1]==c|' ',W)])[2:end],s)
Vim, 46 keystrokes
Ugly, and Hacky.
A <esc>:s/\<\w/:%s\/\0\\c\/\\0/g<cr>:s/ /eg<C-v><Cr>/g<cr>dgg@"