Ontopic Random Computer-Electronics Thread

Jehannum

Eats bananas with the peel still on
Jul 24, 2013
34,548
35,661
723
Albuquerque, NM
Marklar
₥84,231.16
Marklar
M84,231
He would never do anything sensible like

function convertState($name) {
$states = array(
array('name'=>'Alabama', 'abbr'=>'AL'),
array('name'=>'Alaska', 'abbr'=>'AK'),
array('name'=>'Arizona', 'abbr'=>'AZ'),
array('name'=>'Arkansas', 'abbr'=>'AR'),
array('name'=>'California', 'abbr'=>'CA'),
array('name'=>'Colorado', 'abbr'=>'CO'),
array('name'=>'Connecticut', 'abbr'=>'CT'),
array('name'=>'Delaware', 'abbr'=>'DE'),
array('name'=>'Florida', 'abbr'=>'FL'),
array('name'=>'Georgia', 'abbr'=>'GA'),
array('name'=>'Hawaii', 'abbr'=>'HI'),
array('name'=>'Idaho', 'abbr'=>'ID'),
array('name'=>'Illinois', 'abbr'=>'IL'),
array('name'=>'Indiana', 'abbr'=>'IN'),
array('name'=>'Iowa', 'abbr'=>'IA'),
array('name'=>'Kansas', 'abbr'=>'KS'),
array('name'=>'Kentucky', 'abbr'=>'KY'),
array('name'=>'Louisiana', 'abbr'=>'LA'),
array('name'=>'Maine', 'abbr'=>'ME'),
array('name'=>'Maryland', 'abbr'=>'MD'),
array('name'=>'Massachusetts', 'abbr'=>'MA'),
array('name'=>'Michigan', 'abbr'=>'MI'),
array('name'=>'Minnesota', 'abbr'=>'MN'),
array('name'=>'Mississippi', 'abbr'=>'MS'),
array('name'=>'Missouri', 'abbr'=>'MO'),
array('name'=>'Montana', 'abbr'=>'MT'),
array('name'=>'Nebraska', 'abbr'=>'NE'),
array('name'=>'Nevada', 'abbr'=>'NV'),
array('name'=>'New Hampshire', 'abbr'=>'NH'),
array('name'=>'New Jersey', 'abbr'=>'NJ'),
array('name'=>'New Mexico', 'abbr'=>'NM'),
array('name'=>'New York', 'abbr'=>'NY'),
array('name'=>'North Carolina', 'abbr'=>'NC'),
array('name'=>'North Dakota', 'abbr'=>'ND'),
array('name'=>'Ohio', 'abbr'=>'OH'),
array('name'=>'Oklahoma', 'abbr'=>'OK'),
array('name'=>'Oregon', 'abbr'=>'OR'),
array('name'=>'Pennsylvania', 'abbr'=>'PA'),
array('name'=>'Rhode Island', 'abbr'=>'RI'),
array('name'=>'South Carolina', 'abbr'=>'SC'),
array('name'=>'South Dakota', 'abbr'=>'SD'),
array('name'=>'Tennessee', 'abbr'=>'TN'),
array('name'=>'Texas', 'abbr'=>'TX'),
array('name'=>'Utah', 'abbr'=>'UT'),
array('name'=>'Vermont', 'abbr'=>'VT'),
array('name'=>'Virginia', 'abbr'=>'VA'),
array('name'=>'Washington', 'abbr'=>'WA'),
array('name'=>'West Virginia', 'abbr'=>'WV'),
array('name'=>'Wisconsin', 'abbr'=>'WI'),
array('name'=>'Wyoming', 'abbr'=>'WY'),
array('name'=>'Virgin Islands', 'abbr'=>'V.I.'),
array('name'=>'Guam', 'abbr'=>'GU'),
array('name'=>'Puerto Rico', 'abbr'=>'PR')
);

$return = false;
$strlen = strlen($name);

foreach ($states as $state) :
if ($strlen < 2) {
return false;
} else if ($strlen == 2) {
if (strtolower($state['abbr']) == strtolower($name)) {
$return = $state['name'];
break;
}
} else {
if (strtolower($state['name']) == strtolower($name)) {
$return = strtoupper($state['abbr']);
break;
}
}
endforeach;

return $return;
}
:fly:
2020-05-18.jpg
 
  • Haha
Reactions: wetwille

gee

Blame It On The Gassa Nova
Sep 29, 2012
14,280
15,447
373
Halifax
Marklar
₥28,122.81
Marklar
M28,123
I found @fly's production code.

View attachment 11770
In fairness, here's how I'd write it in C.

Code:
#define STATE_COUNT 52

char *alaska_str = "alaska";
char *alaska_abbr_str = "AK";
char *alabama_str = "alabama";
char *alabama_abbr_str = "AB";
// ...

char *state_names[STATE_COUNT] = [
  alaska_str,
  alabama_str,
// ...
];

char *state_abbrs[STATE_COUNT] = [
  alaska_abbr_str,
  alabama_abbr_str,
  // ...
];

char *state_abbr(char *state_name) {
  for (int state = 0; state < STATE_COUNT; state++) {
    if (strcmp(state_name,state_names[state]) == 0) {
      return state_abbrs[state];
    }
    // assume if someone can't spell their state, they're from florida
    return florida_abbr_str;
  }
}
 

Josh

Biff Clurton
Oct 9, 2006
6,787
2,953
273
37
Marklar
₥6,122.72
Marklar
M6,123
In fairness, here's how I'd write it in C.

Code:
#define STATE_COUNT 52

char *alaska_str = "alaska";
char *alaska_abbr_str = "AK";
char *alabama_str = "alabama";
char *alabama_abbr_str = "AB";
// ...

char *state_names[STATE_COUNT] = [
  alaska_str,
  alabama_str,
// ...
];

char *state_abbrs[STATE_COUNT] = [
  alaska_abbr_str,
  alabama_abbr_str,
  // ...
];

char *state_abbr(char *state_name) {
  for (int state = 0; state < STATE_COUNT; state++) {
    if (strcmp(state_name,state_names[state]) == 0) {
      return state_abbrs[state];
    }
    // assume if someone can't spell their state, they're from florida
    return florida_abbr_str;
  }
}
How about in assembly?
 
  • Gravy
Reactions: fly

Josh

Biff Clurton
Oct 9, 2006
6,787
2,953
273
37
Marklar
₥6,122.72
Marklar
M6,123
Back in one of my computer classes in high school, one assignment was to write a batch file to run various utilities like format, scandisk, run commandline antivirus, etc. Then we were given extra credit if we "jazzed" it up using ASCII characters with color output. I really went full bore on the ASCII colors, with shading and what not. I still have the floppy disk with that batch file. I wonder if the disk still works.
 
  • Gravy
Reactions: Valve1138

Jehannum

Eats bananas with the peel still on
Jul 24, 2013
34,548
35,661
723
Albuquerque, NM
Marklar
₥84,231.16
Marklar
M84,231
In fairness, here's how I'd write it in C.

Code:
#define STATE_COUNT 52

char *alaska_str = "alaska";
char *alaska_abbr_str = "AK";
char *alabama_str = "alabama";
char *alabama_abbr_str = "AB";
// ...

char *state_names[STATE_COUNT] = [
  alaska_str,
  alabama_str,
// ...
];

char *state_abbrs[STATE_COUNT] = [
  alaska_abbr_str,
  alabama_abbr_str,
  // ...
];

char *state_abbr(char *state_name) {
  for (int state = 0; state < STATE_COUNT; state++) {
    if (strcmp(state_name,state_names[state]) == 0) {
      return state_abbrs[state];
    }
    // assume if someone can't spell their state, they're from florida
    return florida_abbr_str;
  }
}
I'd just use a hash table. They're plentiful, reliable, they perform O(1), and they're O(n) space.

Yeah, you need to -lglib, but I'm not building for an Arduino.
 

gee

Blame It On The Gassa Nova
Sep 29, 2012
14,280
15,447
373
Halifax
Marklar
₥28,122.81
Marklar
M28,123
I'd just use a hash table. They're plentiful, reliable, they perform O(1), and they're O(n) space.

Yeah, you need to -lglib, but I'm not building for an Arduino.
Bet your hash table isn't as computationally efficient as my linked list. It's literally 8 lines of code, too, so there can't be any bugs in it or issues with it, right?

Code:
char *top_tree[27] = [
  NULL,  // pointer to the abbreviation if this is the end of a tree
  a_tree,  // alabama, alaska, arizona, arkansas
  NULL,
  c_tree,
  d_tree,
  //...
];

char *a_tree[27] = [
  NULL,
  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  al_tree,  // alabama, alaska
  NULL, NULL, NULL, NULL, NULL,
  ar_tree,  // arizona, arkansas
  NULL, NULL, NULL...
];

char *al_tree[27] = [
  NULL,
  ala_tree,  // alabama, alaska
  NULL, NULL,
  //...
];

char *ala_tree[27] = [
  NULL,
  NULL,
  alab_tree,  // alabama
  NULL, NULL, NULL,
  //...
];

char *alab_tree[27] = [
  alabama_abbr,
  NULL, NULL,
  //...
];

char *alabama_abbr = "AB";

char *state_abbr(char *state_str) {
  char *retval = state_abbr_recurse(state_str,top_tree);
  return (retval == NULL) ? florida_abbr : retval;
}

char *state_abbr_recurse(char *str, char *treeptr) {
  if (treeptr[0] != NULL) return treeptr[0];
  return state_abbr_recurse(&str[1],treeptr[str[0] - 96]); // 97 is lower case 'a'
}
 
  • Gravy
Reactions: Jehannum

Jehannum

Eats bananas with the peel still on
Jul 24, 2013
34,548
35,661
723
Albuquerque, NM
Marklar
₥84,231.16
Marklar
M84,231
Bet your hash table isn't as computationally efficient as my linked list. It's literally 8 lines of code, too, so there can't be any bugs in it or issues with it, right?

Code:
char *top_tree[27] = [
  NULL,  // pointer to the abbreviation if this is the end of a tree
  a_tree,  // alabama, alaska, arizona, arkansas
  NULL,
  c_tree,
  d_tree,
  //...
];

char *a_tree[27] = [
  NULL,
  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  al_tree,  // alabama, alaska
  NULL, NULL, NULL, NULL, NULL,
  ar_tree,  // arizona, arkansas
  NULL, NULL, NULL...
];

char *al_tree[27] = [
  NULL,
  ala_tree,  // alabama, alaska
  NULL, NULL,
  //...
];

char *ala_tree[27] = [
  NULL,
  NULL,
  alab_tree,  // alabama
  NULL, NULL, NULL,
  //...
];

char *alab_tree[27] = [
  alabama_abbr,
  NULL, NULL,
  //...
];

char *alabama_abbr = "AB";

char *state_abbr(char *state_str) {
  char *retval = state_abbr_recurse(state_str,top_tree);
  return (retval == NULL) ? florida_abbr : retval;
}

char *state_abbr_recurse(char *str, char *treeptr) {
  if (treeptr[0] != NULL) return treeptr[0];
  return state_abbr_recurse(&str[1],treeptr[str[0] - 96]); // 97 is lower case 'a'
}
I mean, O(1) is O(1), and a linked list isn't O(1)...
 

gee

Blame It On The Gassa Nova
Sep 29, 2012
14,280
15,447
373
Halifax
Marklar
₥28,122.81
Marklar
M28,123
I mean, O(1) is O(1), and a linked list isn't O(1)...
You do have the overhead of the hash function, plus hash tables are best case O(1) and as bad as O(n) if collisions occur.

It's more efficient to write this:

Code:
char *state_abbr_lookup_table[(26*26*26*26*26*26)] = [
  // this is gonna be a long .h file, I think
];

char *state_abbr(char *str) {
  // this fails for states with 5 or less characters for some unknown reason, but those states don't matter anyway
  return state_abbr_lookup_table[(((((str[0]-97)*26+(str[1]-97))*26+(str[2]-97))*26+(str[3]-97))*26+(str[4]-97))*26+(str[5]-97)];
}
 

Jehannum

Eats bananas with the peel still on
Jul 24, 2013
34,548
35,661
723
Albuquerque, NM
Marklar
₥84,231.16
Marklar
M84,231
You do have the overhead of the hash function, plus hash tables are best case O(1) and as bad as O(n) if collisions occur.

It's more efficient to write this:

Code:
char *state_abbr_lookup_table[(26*26*26*26*26*26)] = [
  // this is gonna be a long .h file, I think
];

char *state_abbr(char *str) {
  // this fails for states with 5 or less characters for some unknown reason, but those states don't matter anyway
  return state_abbr_lookup_table[(((((str[0]-97)*26+(str[1]-97))*26+(str[2]-97))*26+(str[3]-97))*26+(str[4]-97))*26+(str[5]-97)];
}
Oh c'mon, as long as your hash function isn't:
Code:
int my_hash_fn(char *key) {
  return 9; //randomly generated!
}
Then you shouldn't really have that many collisions.

The non-embedded programmer in me is yelling, "THE CONSTANTS DON'T MATTER".