Offline real-time how to write Implementation example of the problem in E05 (ruby, C11)

Problem http://nabetani.sakura.ne.jp/hena/orde05dokitruck/ Implementation links http://qiita.com/Nabetani/items/c516875b13a4d282affe is.

so.

There are several implementation policies.

I'm going to choose freely around.

I calculated from behind at once, and the data is a bit.

First, implementation by C11 or C99:

// clang -std=c11 -Wall
// clang x86_64-apple-darwin15.5.0
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <memory.h>

char * build_ans( int s )
{
  char * r = calloc(4, 1);
  for( int i=0 ; i<3 ; ++i ){
    if ( s&(1<<i) ){
      strcat( r, (char[2]){'a'+i});
    }
  }
  if ( !*r ){
    r[0]='-';
  }
  return r;
}

char * solve( char const * src )
{
  int s=7;
  for( char const * it = src + strlen( src ) -1 ; src<=it ; --it ){
    switch(*it-'0'){
    case 1: s|=s/2; break;
    case 2: s|=(s&2)*2+!!(s&4); break;
    case 3: s|=!!(s&4)+(s&1)*2; break;
    case 4: s|=s*2; break;
    case 5: s|=(s&1)*4+(s&4)/2; break;
    case 6: s|=(s&1)*4+!!(s&2); break;
    case 7: s&=5; break;
    case 8: s&=6; break;
    case 9: s&=3; break;
    }
  }
  return build_ans(s);
}

void test( char const * src, char const * expected )
{
  char const * actual = solve( src );
  bool okay = 0==strcmp( actual, expected );
  printf( "%s %s->%s ( e:%s )\n", ( okay ? "ok" : "**NG**" ), src, actual , expected );
  free( actual );
}

int main()
{
  /*0*/ test( "1728398", "bc" );    
  //Abbreviation
  /*36*/ test( "697535114542", "ac" );
  return 0;
}

As usual, most of the test data is omitted.

There are three states, life and death, so it can be expressed in 3 bits. Since the life is set to 1, "& =" brings death and increases the possibility that "| =" will live.

The inside of solve is messed up, but it can't be helped because it expresses the messiness of the blocks that make up the course. It would be cool to have it as data, but this time it is expressed in code. As a reminder, "!!" means a calculation to the effect that logic denial is performed twice and non-zero is set to 1.

The following is a calculation written in ruby with exactly the same purpose. Or rather, I wrote it in ruby earlier.

def apply( s, c )
  case c
  when "0";    s
  when "1";    s | s[1] | s[2]*2
  when "2";    s | s[1]*4 | s[2]
  when "3";    s | s[0]*2 | s[2]
  when "4";    s | s[0]*2 | s[1]*4
  when "5";    s | s[0]*4 | s[2]*2
  when "6";    s | s[0]*4 | s[1]
  when "7";    s & 5
  when "8";    s & 6
  when "9";    s & 3
  end
end

def solve( src )
  r=src.chars.reverse.inject(7) do |s,c|
    apply( s, c )
  end
  %w( a b c ).each.with_index(0).map{ |c,ix|
    r[ix]==0 ? "" : c
  }.join.tap{ |x| return x.empty? ? "-" : x }
end

$stdout.sync=true

DATA.map do |line|
  num, src, expected = line.split(/\s+/)
  actual = solve( src )
  okay = actual==expected
  puts( "%s %2d %s->%s ( e:%s )" % [ ( okay ? "ok" : "**NG**" ), num, src, actual, expected ] )
  okay
end.all?.tap{|x| puts( x ? "everything is ok" : "something wrong" ) }

__END__
0 1728398 bc  
1 789 - 
2 274 ac  
3 185 abc 

36  697535114542  ac  

Even bit operations are easier to write with ruby.

Recommended Posts

Offline real-time how to write Implementation example of the problem in E05 (ruby, C11)
Offline real-time how to write F03 ruby and C implementation example
How to write offline real-time Java implementation example of F01 problem
How to write offline real time Implementation example by ruby and C99 of F04
Offline real-time how to write F06 implementation example
How to set the indent to 2 single-byte spaces in the JAXB implementation of the JDK
How to find the cause of the Ruby error
How to build the simplest blockchain in Ruby
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
Javaer tries to summarize how to write properties in C #
[Ruby on Rails] How to write enum in Japanese
How to write Scala from the perspective of Java
[Ruby] How to find the sum of each digit
[Ruby] How to write blocks
Comparison of how to write Callback function (Java, JavaScript, Ruby)
How to retrieve the hash value in an array in Ruby
[Ruby] How to retrieve the contents of a double hash
How to derive the last day of the month in Java
I tried to solve the Ruby karaoke machine problem (there is an example of the answer)
I tried to solve the Ruby bonus drink problem (there is an example of the answer)
I tried to solve the problem of "multi-stage selection" with Ruby
How to iterate infinitely in Ruby
[Rails] How to write in Japanese
Implementation of ls command in Ruby
I tried to solve the Ruby bingo card creation problem (there is an example of the answer)
How to install Bootstrap in Ruby
How to write ruby if in one line Summary by beginner
How to write when you want to handle "array of C language strings" like argv [] in Ruby-FFI
How to get the id of PRIMAY KEY auto_incremented in MyBatis
I want to change the value of Attribute in Selenium of Ruby
How to get the length of an audio file in java
How to increment the value of Map in one line in Java
How to write the view when Vue is introduced in Rails?
How to change the maximum and maximum number of POST data in Spark
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
How to find the total number of pages when paging in Java
How to constrain the action of the transition destination when not logged in
How to solve the local environment construction of Ruby on Rails (MAC)!
How to change the value of a variable at a breakpoint in intelliJ
I tried to make full use of the CPU core in Ruby
How to get the absolute path of a directory running in Java
How to debug the processing in the Ruby on Rails model only on the console
Android development, how to check null in the value of JSON object
[Ruby On Rails] How to search the contents of params using include?
[Ruby] How to prevent errors when nil is included in the operation
How to insert processing with any number of elements in iterative processing in Ruby
How to get date data in Ruby
Summary of how to write annotation arguments
How to determine the number of parallels
How to sort the List of SelectItem
How to get the date in java
Write the movement of Rakefile in the runbook
[Swift] How to get the number of elements in an array (super basic)
[Ruby on Rails] How to make the link destination part of the specified id
graphql-ruby: How to get the name of query or mutation in controller Note
[Ruby] Learn how to use odd? Even? And count the even and odd numbers in the array!
How to get the ID of a user authenticated with Firebase in Swift
I tried to solve the tribonatch sequence problem in Ruby (time limit 10 minutes)
How to check the latest version of io.spring.platform to describe in pom.xml of Spring (STS)
How to resolve errors that occur in the "Ruby on Rails" integration test
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed