19
3
The simple part: Given an input string containing only printable ASCII-characters (space - tilde), count the number of occurrences of each character and return the result on any convenient format. The result for a string a%hda7a
should be something like: a:3, %:1, h:1, 7:1, d:1
. Sorting is unnecessary, the delimiters and formats are optional but it must be easily understood which number corresponds to which character. You shall not include characters that are not in the input string (a:3, b:0, c:0, d:1, ...
is not OK).
The real challenge:
Convert every character in your code to an 8-bit binary number (or 16-bit if you're using UTF-16 or similar), and enumerate every character starting at 0
.
For every character (i
is the enumerator), the i%7
-bit1 must be 1
. The bits are numbered from the right. All other bits can be whatever you want.
Let's use the following code as an example:
[f]-xif)#f
Converting this to binary we get the array below. The first number (representing [
has a 1
in the 0'th position, so that one is OK. The second number (representing f
has a 1
in the 1'st position, so that one is OK too. Continue like this, and you'll see that the code above is valid.
C 76543210 Bit number - -------- ---------- [ 01011011 0 - OK f 01100110 1 - OK ] 01011101 2 - OK - 00101101 3 - OK x 01111000 4 - OK i 01101001 5 - OK f 01100110 6 - OK ) 00101001 0 - OK # 00100011 1 - OK f 01100110 2 - OK
If we change the code into: ]f[-xif)#f
we'll get the following start of the sequence:
C 76543210 Bit number
- -------- ----------
] 01011101 0 <- OK
f 01100110 1 <- OK
[ 01011011 2 <- Not OK
- 00101101 3 <- OK
As we see, the third character [
doesn't have a 1
in the 2nd position (zero-indexed), and this code is therefore not valid.
Test cases:
Input:
This is a string containing some symbols: ".#!".#&/#
Output:
! " # & / : T a b c e g h i l m n o r s t y .
7 1 2 3 1 1 1 1 2 1 1 1 2 1 5 1 2 4 3 1 6 2 1 2
Any reasonable output format is OK (whatever is most convenient to you). You could for instance have: :7, !:1, ":2, #:3, &:1, /:1, T:1, a:2 ...
or [ ,7][!,1][",2][#,3][&,1]...
. The output is on any standard way (return from function, printed to STDOUT etc.)
1i
modulus 7
.
This is code-golf, so the shortest code in bytes will winref.
6
To be of a little assistance, here are the characters you're allowed to use in the
– TidB – 2016-12-23T12:26:32.633n%7
th spot > http://pastie.org/pastes/10985263/text@TidB the website is offline?? – Rod – 2016-12-23T13:07:32.023
1
@Rod Yeah, pastie seems to have some problems. Try pastebin instead
– TidB – 2016-12-23T13:10:43.9631Remember that newline is
00001010
. It can be useful too! :) – Stewie Griffin – 2016-12-23T13:12:33.413Yeah, I only included the printable ASCII characters that you can identify quickly. Tabs, newlines etc. aren't included, not speaking about the extensive charset of esoteric languages :D – TidB – 2016-12-23T13:13:50.067
1
To be of some further assistance, here's a validation script you can use for UTF-8 encodings. Just encapsulate the input in a string like the example.
– AdmBorkBork – 2016-12-23T14:19:11.987How far can we take the reasonable output format? E.g., all counts newline separated followed by a "key" of all the characters in order of their counts? – Sanchises – 2016-12-23T14:54:22.613
If you have 90 different input characters then it will be hard to tell which key belongs to which character, so I think I must say no to that one @Sanchises. – Stewie Griffin – 2016-12-23T15:01:08.563
OK thanks. I think my current submission should be fine then. – Sanchises – 2016-12-23T16:23:27.233
Apologies for the lacking replies, I'm celebrating Christmas with the family and code golf is not the main priority right now. Will be back :-) – Stewie Griffin – 2016-12-24T09:13:08.843