| Bytes | Lang | Time | Link |
|---|---|---|---|
| 063 | Python | 240927T100406Z | pxeger |
| 1004 | Whitespace | 240926T231759Z | TallMoos |
| 035 | APLDyalog Unicode | 240924T033239Z | akamayu |
| 073 | Haskell | 240923T222856Z | Antonio |
| 091 | Python | 240923T184232Z | TallMoos |
| 044 | Perl 5 pl | 240923T195249Z | Xcali |
| 048 | JavaScript Node.js | 240922T225207Z | l4m2 |
| 020 | MATL | 240923T103216Z | Luis Men |
| 033 | Charcoal | 240923T084717Z | Neil |
| 021 | 05AB1E | 240923T083207Z | Kevin Cr |
Python, 63 bytes
lambda s,n:"".join(c*-~(c<"!" and [n&1,n:=n//2][0]) for c in s)
Neatly spaced keywords to ensure it contains 7 spaces.
Whitespace, 1004 bytes
a b c d
This took a minute.
This uses a loophole in the language implementation to allow a trivial solution to the challenge twist, where all but the 3 white space characters are ignored by the interpreter. This means the top instruction (push 10922 onto the stack) is enough to satisfy the twist, since there are the characters a, b, c, and d inserted in remove any consecutive spaces from the source code. I did come up with a non-trivial solution, which would be much more intensive to implement. The call subroutine operation is LF SPC TAB, while the mark label is LF SPC SPC. That means you could make the top instruction of the program 'call subroutine 10922', which might be transformed into either 'mark label 10922', or transform the label itself (10922 in binary is 10101010101010). If there were all 128 possible transformations of that label at the bottom, immediately followed by the return from subroutine operation, it would effectively be a no-op regardless of the transformation.
Explanation
Unfortunately TIO only has version 0.2, so this program cannot use the copy or slide operators. That means it can only use 2 local variables at a time. I did implement a few subroutines to allow the use of heap address 1 and 2 as pseudo registers, which helped.
APL(Dyalog Unicode), 35 bytes SBCS
Assumes ⎕io←0.
{⍺ ⌿⍨ 1 + ((+\ × ⊢) ' '=⍺)∊⍸⍵⊤⍨8⍴2}
{⍺ ⌿⍨ 1 + ((+\ × ⊢) ' '=⍺)∊⍸⍵⊤⍨8⍴2}
⍺ ⌿⍨ ⍝ input string replicated with
1 + ⍝ one more of
((+\ × ⊢) ' '=⍺)∊⍸⍵⊤⍨8⍴2 ⍝ mask for spaces that need to be doubled
(+\ × ⊢) ' '=⍺ ⍝ i if it's the i-th space (1-indexed), 0 for non-spaces
⍝ 'x xxx xxxx x' → 0 1 0 0 0 2 0 0 0 0 3 0
∊ ⍝ is in
⍸⍵⊤⍨8⍴2 ⍝ which bits are 1 in 8-bit representation of ⍵
⍝ We take 8-bit representation so the highest possible set bit becomes 1.
The corresponding decoding function is {2⊥¯1++/↑7↑⊆⍨' '=⍵}.
Haskell, 73 bytes
f(' ':y)n=(replicate(mod n 2+1)' ')++f y(div n 2);f(x:y)n=x:f y n;f s 0=s
Python, 93 92 91 bytes
lambda s, n : ''.join( [ c +c*((1<<s[:i].count(' ')&n>0)&(c==' '))for i,c in enumerate(s)])
There are exactly 7 spaces before the .count(' ') so it should not be affected for integer inputs <= 128 when feeding itself as the source string
Ungolfed
def ungolfed(input_string, secret_int_to_encode):
return ''.join(
[
character # Always return the base character
+ # Optionally adding the same character multiplied by a value of 0 or 1 based on the challenge conditions
character * (
(
1 << input_string[:string_index].count(' ') # The number 1 bitshifted by the number of spaces in the string so far.
# This acts as a bitmask against secret_int_to_encode
&
secret_int_to_encode # Bitwise AND here will return non-zero if the bit representation of the integer has a 1 at this index
> 0 # Comparing against 0 to normalize the value as 0 or 1, as opposed to whatever bit of secret_to_encode we might be at
) # This value is 1 if the bitwise representation of the integer to encode has a 1 at this space-based index
&
(character==' ') # Bitwise AND with 1 if the current character is a whitespace.
# This will null the second character when we are iterating on a char not equal to ' '
) for string_index , character in enumerate(input_string)
]
)```
Perl 5 -pl, 44 bytes
$a = sprintf "%b", <>; s/$"\K/$"x chop $a/ge
Takes the message on the first input line, the number on the second.
Encodes the friend number in binary in the first 7 spaces, least significant bit first, with one space representing 0 and two spaces representing 1.
JavaScript (Node.js), 48 bytes
x => n => x . replace (/ /g,c=>2*(n/=2)&1?c+c:c)
-2 B Arnauld
The 8th space would never get doubled so it work correctly
MATL, 20 bytes
t f y 5W=f iB ) hS )
Spaces have no effect on the above code, and can be placed anywhere.
Explanation
t % Implicit input: string. Duplicate
f % Find: gives [1 2 ... L], where L is the string length
y % Push another copy of the string
5 % Push 5
W % Exponential with base 2: gives 32
= % Compare, element-wise. Gives true for spaces, false otherwise
f % Find: gives indices of spaces
i % Input: number
B % Binary expansion. Gives true/false for the bits
) % Use as logical index. Gives indices of spaces selected by the binary expansion
h % Concatenate horizontally. Attaches those indices to the original [1 2 ... L]
S % Sort
) % Use as indices into the stting. Implicit display
Charcoal, 33 bytes
Nθ¿ ¿ ¿ ¿ ¿ ⭆η⎇›ι ι… ⊕﹪÷θX²№…ηκ ²
Try it online! Link is to verbose version of code. Explanation:
Nθ
Input n.
¿ ¿ ¿ ¿ ¿
Filler commands that do nothing but are required for the source layout. (They're actually if statements, but strings of one or two spaces are both truthy anyway.)
⭆η⎇›ι ι… ⊕﹪÷θX²№…ηκ ²
Map over the message, replacing characters that are not greater than one or two spaces (this always covers the rest of printable ASCII) with one or two spaces truncated or cyclically extended to one or two spaces according to the next least significant bit of the input number. The bit index is found by counting spaces so far so this has to be the eighth space for that to work correctly (adding a two byte filler is golfier than any other way of working around it).
05AB1E, 24 21 bytes
.γ ð Q } I b € 0ÅϺ}J
Try it online or verify all test cases.
Explanation:
.γ # Adjacent group-by on the first (implicit) input-string:
ðQ # Check if the current character is a space
}I # After the group-by: push the second input-integer
b # Convert it to a binary-string
€0 # Put a 0 before each bit (and convert it to a binary-list)
ÅÏ # Apply on the truthy (==1) positions in the grouped list:
º # Horizontal mirror, to double the space
}J # After the truthy-apply: join it back together to a string
# (which is output implicitly as result)
The no-op spaces to comply to the challenge rules can be added anywhere in the code, except between the .γ and ÅÏ, since those are single 2-byte builtins.