octetta http://octetta.com making cool sounds since birth Sun, 30 Oct 2016 21:49:56 +0000 en-US hourly 1 https://wordpress.org/?v=4.7.7 37872614 “Random Facebook quiz” (part 3/3) http://octetta.com/random-facebook-quiz-part-33/ http://octetta.com/random-facebook-quiz-part-33/#respond 24options
Keyds Vanuatu recensioni
Sun, 31 May 2015 15:30:03 +0000
http://octetta.com/?p=233

Well I can’t leave this alone. Here’s a ‘C’ version:


#include <stdio.h>
#include <string.h>
#include <malloc.h>
char *words[] = {
  "tsar", "rat", "tar", "star",
  "tars", "cheese", "mow", "lox",
  NULL
};
#define LETTERS 26
// the following assumes the alphabet is no more than 256 symbols
static char hashcount[LETTERS];
// the following assumes a word's symbols never repeats more than 256
static char hashbuffer[LETTERS*3+1];
char *hash(char *word) {
  char *ptr = word;
  int i;
  for (i=0; i<LETTERS; i++) hashcount[i] = 0;
  while (*ptr) {
    char c = tolower(*ptr);
    if (c >= 'a' && c <= 'z') hashcount[c-'a']++;
    ptr++;
  }
  ptr = hashbuffer;
  for (i=0; i<LETTERS; i++) {
    if (hashcount[i]) {
      *ptr++ = i + 'a';
      if (hashcount[i] > 1) {
        sprintf(ptr, "%x", hashcount[i]);
        ptr += (hashcount[i]/16 + 1);
      }
    }
  }
  *ptr = '\0';
  return strdup(hashbuffer);
}
int main(int argc, char *argv[]) {
  char **id = NULL;
  char *printed = NULL;
  int i, j, len=0;
  while (1) {
    if (words[len] == NULL) break;
    len++;
  }
  id = (char **)malloc(len*sizeof(char *));
  printed = (char *)malloc(len*sizeof(char));
  for (i=0; i<len; i++) {
    id[i] = hash(words[i]);
    printed[i] = 0;
    printf("[%d] %s : %s\n", i, words[i], id[i]);
  }
  for (i=0; i<len; i++) {
    if (printed[i] == 0) {
      printed[i] = 1;
      printf("===\n");
      printf("%s\n", words[i]);
      for (j=i+1; j<len; j++) {
        if (printed[j] == 0 && (strcmp(id[i], id[j]) == 0)) {
          printed[j] = 1;
          printf("%s\n", words[j]);
        }
      }
    }
  }
  for (i=0; i<len; i++) free(id[i]);
  free(id);
  free(printed);
  return 0;
}

…and its output:


[0] tsar : arst
[1] rat : art
[2] tar : art
[3] star : arst
[4] tars : arst
[5] cheese : ce3hs
[6] mow : mow
[7] lox : lox
===
tsar
star
tars
===
rat
tar
===
cheese
===
mow
===
lox
]]>
http://octetta.com/random-facebook-quiz-part-33/feed/ 0 233
“Random Facebook quiz” (part 2/3) http://octetta.com/random-facebook-quiz-part-22/ http://octetta.com/random-facebook-quiz-part-22/#respond Sun, 31 May 2015 01:45:06 +0000 http://octetta.com/?p=229 PhiPi from the Propeller forums pointed out that my simple id() function would treat the words “MOW” and “LOX” as the same. This means my code “accidently worked” for the dataset provided in the question.

PhiPi made a suggestion that I ran with. I tweaked the id() function and came up with this:


t = { "tsar", "rat", "tar", "star", "tars", "cheese", }
function id(s)
        local t = {}
        for i=1,26 do t[i] = 0 end
        for i=1,#s do
                local c = string.lower(s:sub(i,i))
                if c >= "a" and c <= "z" then
                        local h = string.byte(c) - ("a"):byte() + 1
                        if h > 0 then t[h] = t[h] + 1 end
                end
        end
        local r = ""
        for i=1,26 do
                if t[i] > 0 then
                        r = r .. string.char(("a"):byte() + i - 1)
                end
                if t[i] > 1 then
                        r = r .. t[i]
                end
        end
        return r
end
p = {} u = {}
print("* word -> id *")
for i=1,#t do
        p[i] = 0
        u[i] = id(t[i])
        print(t[i], u[i])
end
for i=1,#u do
        if p[i] == 0 then
                p[i] = 1
                print("===")
                print(t[i])
                for j=i+1,#u do
                        if p[j] == 0 and u[i] == u[j] then
                                p[j] = 1
                                print(t[j])
                        end
                end
        end
end

which outputs:


* word -> id *
tsar    arst
rat     art
tar     art
star    arst
tars    arst
cheese  ce3hs
===
tsar
star
tars
===
rat
tar
===
cheese

]]>
http://octetta.com/random-facebook-quiz-part-22/feed/ 0 229
“Random Facebook quiz” (part 1/3) http://octetta.com/random-facebook-quiz/ http://octetta.com/random-facebook-quiz/#respond Sat, 30 May 2015 20:37:19 +0000 http://octetta.com/?p=222 My friends on the Parallax Propeller forum posted the following quiz:

During a 45-minute Facebook job interview you could be requested to write a C++ from scratch that does this:
You have a list of these words
“tsar”, “rat”, “tar”, “star”, “tars”, “cheese”

re-arrange them so the anagrams (words that have the same letters) are grouped together.
output order not important.

Here’s my take (albeit in Lua instead of C++):



t = { "tsar", "rat", "tar", "star", "tars", "cheese", }
function id(s)
local n=0
for i=1,#s do
n = n + s:byte(i,i)
end
return n
end
u = {}
p = {}
for i=1,#t do
p[#p+1] = 0
u[#u+1] = id(t[i])
end
for i=1,#u do
if p[i] == 0 then
p[i] = 1
print("===")
print(t[i])
for j=1,#u do
if p[j] == 0 and u[i] == u[j] then
p[j] = 1
print(t[j])
end
end
end
end

…and the output…



===
tsar
star
tars
===
rat
tar
===
cheese

]]>
http://octetta.com/random-facebook-quiz/feed/ 0 222
“Solution to Problem 4” http://octetta.com/solution-to-problem-4/ http://octetta.com/solution-to-problem-4/#respond Mon, 11 May 2015 01:52:48 +0000 http://octetta.com/?p=202 A Lua response to Problem 4:

Write a function that given a list of non negative integers, arranges them such that they form the largest possible number. For example, given [50, 2, 1, 9], the largest formed number is 95021.

function eval(a, i)
if not i then i = 1 max = 0 end
if i == #a then
local n = tonumber(table.concat(a))
if n &gt; max then max = n end
else
for j=i,#a do
a[i],a[j] = a[j],a[i]
eval(a, i+1)
a[i],a[j] = a[j],a[i]
end
end
end
eval({50, 2, 1, 9})
io.write("max is ", max, "\n")

Output is:

max is 95021

]]>
http://octetta.com/solution-to-problem-4/feed/ 0 202
“Solution to Problem 5” http://octetta.com/solution-to-problem-5/ http://octetta.com/solution-to-problem-5/#respond Mon, 11 May 2015 01:18:53 +0000 http://octetta.com/?p=195 A dude recently posted some programming challenges on his blog (here) that were mentioned on Reddit r/programming.

Problem 5 caught my eye and below, I’ve posted a short and sweet Lua brute-force to solve it:

Write a program that outputs all possibilities to put + or – or nothing between the numbers 1, 2, …, 9 (in this order) such that the result is always 100. For example: 1 + 2 + 34 – 5 + 67 – 8 + 9 = 100.

a = {
1, "", 2, "", 3, "", 4, "", 5, "",
6, "", 7, "", 8, "", 9
}
function inc(a)
for i=2,#a,2 do
if a[i] == "" then a[i] = "+" return true end
if a[i] == "+" then a[i] = "-" return true end
if a[i] == "-" then a[i] = "" end
end
return false
end
function eval(a)
local s = table.concat(a)
if loadstring("return " .. s)() == 100 then print(s) end
end
repeat eval(a) until inc(a) == false

Output looks like:

123-45-67+89
12-3-4+5-6+7+89
12+3+4+5-6-7+89
123+4-5+67-89
1+2+3-4+5+6+78+9
12+3-4+5+67+8+9
1+23-4+56+7+8+9
1+2+34-5+67-8+9
1+23-4+5+6+78-9
123+45-67+8-9
123-4-5-6-7+8-9

]]>
http://octetta.com/solution-to-problem-5/feed/ 0 195
Expensive tool… great results… worth it! http://octetta.com/expensive-tool-great-results-worth-it/ http://octetta.com/expensive-tool-great-results-worth-it/#comments Sat, 04 Oct 2014 21:58:53 +0000 http://octetta.com/?p=184 I bit the bullet and bought the “Engineer PA-09” crimping pliers for my board connectors. Just made a sample cable and I’m already pleased with the results. Totally worth the cost.

]]>
http://octetta.com/expensive-tool-great-results-worth-it/feed/ 1 184
Boards part 3 http://octetta.com/boards-part-3/ http://octetta.com/boards-part-3/#respond Tue, 16 Sep 2014 12:30:51 +0000 http://octetta.com/?p=172 8-channel DAC and some connectors were added.
oct-midi-dac

]]>
http://octetta.com/boards-part-3/feed/ 0 172
Boards part 2 http://octetta.com/boards-part-2/ http://octetta.com/boards-part-2/#respond Mon, 15 Sep 2014 13:22:28 +0000 http://octetta.com/?p=167 Opto isolator and 5-pin DIN connector.
oct-midi

]]>
http://octetta.com/boards-part-2/feed/ 0 167
Boards part 1 http://octetta.com/knobs-part-1/ http://octetta.com/knobs-part-1/#respond Tue, 02 Sep 2014 03:47:50 +0000 http://octetta.com/?p=152 Two rotary encoders and two potentiometers.

oct-knobs

]]>
http://octetta.com/knobs-part-1/feed/ 0 152
MIDI IN http://octetta.com/midi-in/ http://octetta.com/midi-in/#respond Tue, 02 Sep 2014 03:42:11 +0000 http://octetta.com/?p=139 1st

2nd

]]>
http://octetta.com/midi-in/feed/ 0 139