How can I generate this number pattern?

Inputs 1-32 entered, how can I generate the output below?

in. of

  • 1
  • 1
  • 1
  • 1
  • 2
  • 2
  • 2
  • 2
  • 1
  • 1
  • 1
  • 1
  • 2
  • 2
  • 2
  • 2 ...

Edit Not homework .. just lack of sleep.

I work in C #, but I was looking for an agnostic language algorithm.

Edit 2 To provide a little more background ... I have an array of 32 elements, which is a two-dimensional chessboard. I needed the last part of this algorithm to convert between a vector and a graph, where the index is aligned on black squares on a chessboard.

Final code:

 --Index;
 int row = Index >> 2;
 int col = 2 * Index - (((Index & 0x04) >> 2 == 1) ? 2 : 1);
+5
source share
15 answers

, , , , 0-31, ( 1 )

?

0x0000 -> 1
0x0001 -> 1
0x0010 -> 1
0x0011 -> 1
0x0100 -> 2
0x0101 -> 2
0x0110 -> 2
0x0111 -> 2
0x1000 -> 1
0x1001 -> 1
0x1010 -> 1
0x1011 -> 1
0x1100 -> 2
...

, , 0, 1 , 1, 2

:

char codify(char input)
{
     return ((((input-1)&0x04)>>2 == 1)?(2):(1));
}

,

char codify(char input)
{
     return ((input-1 & 0x04)?(2):(1));
}

(, C) 0 false true. , #, . , - , C-!

+16

C:

char output = "11112222"[input-1 & 7];

char output = (input-1 >> 2 & 1) + '1';

FogleBird:

char output = input - 1 & 4 ? '2' : '1';

:

char output = '2' - (0x1e1e1e1e >> input & 1);

char output = "12"[input-1>>2&1];

. : -)

+10

2 (-): , 1-, 3-, 5- .. 1, 2-, 4-, 6- 2.

s := ((n-1) div 4) mod 2;
return s + 1;

div .

EDIT: div,

+6

, , 1..32 , :

// binary 1111 0000 1111 0000 1111 0000 1111 0000
const uint32_t lu_table = 0xF0F0F0F0;

// select 1 bit out of the table
if (((1 << (input-1)) & lu_table) == 0) {
    return 1;
} else {
    return 2;
}

, , . , , , , , ( ), . , , .

+6

Python

def f(x):
    return int((x - 1) % 8 > 3) + 1

:

def f(x):
    return 2 if (x - 1) & 4 else 1

:

def f(x):
    return (((x - 1) & 4) >> 2) + 1
+4

return ((((input-1)&0x04)>>2 == 1)?(2):(1)); , :

return 1 + ((input-1) & 0x04 ) >> 2;

+4

Perl:

#!/usr/bin/perl

use strict; use warnings;

sub it {
    return sub {
        my ($n) = @_;
        return 1 if 4 > ($n - 1) % 8;
        return 2;
    }
}

my $it = it();

for my $x (1 .. 32) {
    printf "%2d:%d\n", $x, $it->($x);
}

:

sub it {
    return sub {
        my ($n) = @_;
        use integer;
        return 1 + ( (($n - 1) / 4) % 2 );
    }
}
+3

Haskell:

vec2graph :: Int -> Char
vec2graph n = (cycle "11112222") !! (n-1)
+3

, .

VB.NET - :

for i as integer = 1 to 32
   dim intAnswer as integer = 1 + (Math.Floor((i-1) / 4) mod 2)
   ' Do whatever you need to do with it
next

, , sigle.

+1

:

if (input == "1") {Console.WriteLine(1)};
if (input == "2") {Console.WriteLine(1)};
if (input == "3") {Console.WriteLine(1)};
if (input == "4") {Console.WriteLine(1)};
if (input == "5") {Console.WriteLine(2)};
if (input == "6") {Console.WriteLine(2)};
if (input == "7") {Console.WriteLine(2)};
if (input == "8") {Console.WriteLine(2)};

....

+1

Groovy:

def codify = { i  ->
    return  (((((i-1)/4).intValue()) %2 ) + 1)
}

:

def list = 1..16
list.each {
    println "${it}: ${codify(it)}"
}
+1
char codify(char input)
{
     return  (((input-1) & 0x04)>>2) + 1;
}
+1

Python:

output = 1
for i in range(1, 32+1):
  print "%d. %d" % (i, output)
  if i % 4 == 0:
    output = output == 1 and 2 or 1
+1

JavaScript

output = ((input - 1 & 4) >> 2) + 1;

drhirsch JavaScript:

output = input - 1 & 4 ? 2 : 1;

( FogleBird):

output = -~((input - 1) % 8 > 3);
+1

Java, modulo ('%'), (0,1,2... 7), , 1 (?) 2 (:) . ...

 public static void main(String[] args) {
        for (int i=1;i<=32;i++) {
             System.out.println(i+"="+ (i%8<4?1:2) );
    }

:

1 = 1 2 = 1 3 = 1 4 = 2 5 = 2 6 = 2 7 = 2 8 = 1 9 = 1 10 = 1 11 = 1 12 = 2 13 = 2 14 = 2 15 = 2 16 = 1 17 = 1 18 = 1 19 = 1 20 = 2 21 = 2 22 = 2 23 = 2 24 = 1 25 = 1 26 = 1 27 = 1 28 = 2 29 = 2 30 = 2 31 = 2 32 = 1

+1
source

All Articles