2 C poziom 7-6

11 Drying Potatoes

All we eat is water and dry matter.

John bought potatoes: their weight is 100 kilograms. Potatoes contain water and dry matter.

The water content is 99 percent of the total weight. He thinks they are too wet and puts them in an oven - at low temperature - for them to lose some water.

At the output the water content is only 98%.

What is the total weight in kilograms (water content plus dry matter) coming out of the oven?

He finds 50 kilograms and he thinks he made a mistake: "So much weight lost for such a small change in water content!"

Can you help him?

Write function potatoes with

int parameter p0 - initial percent of water-
int parameter w0 - initial weight -
int parameter p1 - final percent of water -
potatoesshould return the final weight coming out of the oven w1 truncated as an int.

Example:

potatoes(99, 100, 98) --> 50

#include <stdio.h>

int potatoes(int p0, int w0, int p1)
{
    return (w0*(100-p0))/(100-p1);
}

int main(void)
{
    printf("%d\n",potatoes(99,100,98));
    return 0;
}


12 Moves in squared strings (I) (7)

This kata is the first of a sequence of four about "Squared Strings".

You are given a string of n lines, each substring being n characters long: For example:

s = "abcd\nefgh\nijkl\nmnop"

We will study some transformations of this square of strings.

Vertical mirror: vert_mirror (or vertMirror or vert-mirror)
vert_mirror(s) => "dcba\nhgfe\nlkji\nponm"
Horizontal mirror: hor_mirror (or horMirror or hor-mirror)
hor_mirror(s) => "mnop\nijkl\nefgh\nabcd"
or printed:

vertical mirror   |horizontal mirror 
abcd --> dcba     |abcd --> mnop
efgh     hgfe     |efgh     ijkl
ijkl     lkji     |ijkl     efgh
mnop     ponm     |mnop     abcd
#Task:

Write these two functions
and

high-order function oper(fct, s) where

fct is the function of one variable f to apply to the string s (fct will be one of vertMirror, horMirror)
#Examples:

s = "abcd\nefgh\nijkl\nmnop"
oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm"
oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd"
Note:
The form of the parameter fct in oper changes according to the language. You can see each form according to the language in "Sample Tests".

Bash Note:
The input strings are separated by , instead of \n. The ouput strings should be separated by \r instead of \n. See "Sample Tests".


#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>

char s[] = "abcd\nefgh\nijkl\nmnop";

char* vertMirror(char* strng) {

int dl=strlen(strng);
char *wynik=malloc(dl+1);

for(int i=0;i<dl;i++)
    wynik[i]='X';


int n;
for (int i=0;i<dl;i++) //find n
    if(strng[i]=='\n')
    {
        n=i;
        break;
    }

for(int i=0;i<n;i++)      //copy strng  to wynik (not coping \n)
{
    for(int j=0;j<n;j++)
    {

        wynik[i*n+i+j]=strng[(i*n+i+(n-1-j))]; //copy and reverse every n

    }
}


    for(int i=1;i<=dl;i++)    //put '\n'  every 5(n+1) per starting n
{
    int a=i%(n+1);
    if(!a)
        //printf("%d\n",i-1);
        wynik[i-1]='\n';
}

wynik[dl]=0;


    return wynik;
}
char* horMirror(char* strng) {

int dl=strlen(strng);
char *wynik=malloc(dl+1);

for(int i=0;i<dl;i++)
    wynik[i]='X';


int n;
for (int i=0;i<dl;i++) //find n
    if(strng[i]=='\n')
    {
        n=i;
        break;
    }

for(int i=0;i<n;i++)      //copy strng  to wynik (not coping \n)
{
    for(int j=0;j<n;j++)
    {

        wynik[i*n+i+j]=strng[((n-1-i)*n+(n-1-i)+j)]; //copy and mirror grop of n

    }
}


    for(int i=1;i<=dl;i++)    //put '\n'  every 5(n+1) per starting n
{
    int a=i%(n+1);
    if(!a)
        //printf("%d\n",i-1);
        wynik[i-1]='\n';
}

wynik[dl]=0;


    return wynik;
}

typedef char* (*generic_func_t) (char*);

char* oper(generic_func_t f, char* s) {
    return f(s);
}


int main(void)
{
    //printf("%s\n",vertMirror(s));
    //printf("%s\n",horMirror(s));
printf("%s\n",oper(vertMirror,s));



    return 0;
}
.


13 Moves in squared strings (II)(6)

You are given a string of n lines, each substring being n characters long: For example:

s = "abcd\nefgh\nijkl\nmnop"

We will study some transformations of this square of strings.

Clock rotation 180 degrees: rot
rot(s) => "ponm\nlkji\nhgfe\ndcba"
selfie_and_rot(s) (or selfieAndRot or selfie-and-rot) It is initial string + string obtained by clock rotation 180 degrees with dots interspersed in order (hopefully) to better show the rotation when printed.
s = "abcd\nefgh\nijkl\nmnop" -->
"abcd....\nefgh....\nijkl....\nmnop....\n....ponm\n....lkji\n....hgfe\n....dcba"
or printed:
|rotation        |selfie_and_rot
|abcd --> ponm   |abcd --> abcd....
|efgh     lkji   |efgh     efgh....
|ijkl     hgfe   |ijkl     ijkl.... 
|mnop     dcba   |mnop     mnop....
                           ....ponm
                           ....lkji
                           ....hgfe
                           ....dcba
#Task:

Write these two functions rotand selfie_and_rot
and

high-order function oper(fct, s) where

fct is the function of one variable f to apply to the string s (fct will be one of rot, selfie_and_rot)
#Examples:

s = "abcd\nefgh\nijkl\nmnop"
oper(rot, s) => "ponm\nlkji\nhgfe\ndcba"
oper(selfie_and_rot, s) => "abcd....\nefgh....\nijkl....\nmnop....\n....ponm\n....lkji\n....hgfe\n....dcba"
Notes:
The form of the parameter fct in oper changes according to the language. You can see each form according to the language in "Your test cases".
It could be easier to take these katas from number (I) to number (IV)
Forthcoming katas will study other tranformations.

Bash Note:
The input strings are separated by , instead of \n. The ouput strings should be separated by \r instead of \n. See "Sample Tests".

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>

char s[] = "abcdv\nefghv\nijklv\nmnopv\nkolev";

char *rot(char* strng) {

int dl=strlen(strng);
char *wynik=malloc(dl+1);
char *wynik2=malloc(dl+1);

int n;
for (int i=0;i<dl;i++) //find n
    if(strng[i]=='\n')
    {
        n=i;
        break;
    }

for(int i=0;i<n;i++)      //copy strng  to wynik (not coping \n)
{
    for(int j=0;j<n;j++)
    {
        int aa=i*n+i+j;
        wynik[i*n+i+j]=strng[(i*n+i+(n-1-j))]; //copy and reverse every n
    }
}


for(int i=0;i<n;i++)      //copy strng  to wynik (not coping \n)
{
    for(int j=0;j<n;j++)
    {
        wynik2[i*n+i+j]=wynik[((n-1-i)*n+(n-1-i)+j)]; //copy and mirror grop of n
    }
}


    for(int i=1;i<=dl;i++)    //put '\n'  every 5(n+1) per starting n
{
    int a=i%(n+1);
    if(!a)
    wynik2[i-1]='\n';
}

wynik2[dl+1]=0;

    return wynik2;
}

char* selfieAndRot(char* strng) {
int dl=strlen(strng);

int n;
for (int i=0;i<dl;i++) //find n
    if(strng[i]=='\n')
    {
        n=i;
        break;
    }

int dlw=4*dl-(2*n-2);
char *wynik=malloc(dlw+1);


for (int i=0;i<dlw;i++)
    wynik[i]='X';



for(int i=0;i<n;i++)      //copy strng  to wynik (not coping \n)
{
    for(int j=0;j<n;j++)
    {
        wynik[i*2*n+i+j]=strng[(i*n+i+j)]; //copy NO reverse every n
    }
    for(int k=0;k<n;k++)
        wynik[i*2*n+n+i+k]='.';
}



for(int i=0;i<2*n;i++)      //copy strng  to wynik (not coping \n) AND mirror
{
    for(int j=0;j<2*n;j++)
    {
        int aa=dlw-(i*n+i+j);
        wynik[dlw-(i*n+i+j)]=wynik[i*n+i+j]; //copy and mirror grop of n
    }
}




    for(int i=1;i<=dlw;i++)    //put '\n'  every 5(n+1) per starting n
{
    int a=i%(2*n+1);
    if(!a)
    wynik[i-1]='\n';
}

wynik[dlw+1]=0;
return wynik;
}

typedef char* (*generic_func_t) (char*);

char* oper(generic_func_t f, char* s) {
   return f(s);
}


int main(void)
{
    //printf("%s\n",rot(s));
    //printf("%s\n",selfieAndRot(s));
printf("%s\n",oper(rot,s));
printf("%s\n",oper(selfieAndRot,s));

    return 0;
}


14 





1 C poziom 8-7 kyu

1

Complete the solution so that it returns true if the first argument(string) passed in ends with the 2nd argument (also a string).

Examples:

solution("abc", "bc") # returns true
solution("abc", "d") # returns false

#include <stdio.h>
#include <stdlib.h>
//#include <string.h>
#include <stdbool.h>

bool solution(const char *string, const char *ending)
{
    bool war=true;
    int dl1=strlen(string);
    int dl2=strlen(ending);
    if (dl2>dl1)
        return false;
    dl1-=1;
    dl2-=1;
    for (int i=dl2;i>=0;i--)
    {
        if(string[dl1--]!=ending[i])
           war=false;
    }


    return war;
}


int main()
{
 bool wynik=solution("abc","bc");
    printf("wynik %d\n",wynik);
    wynik=solution("abc","d");
    printf("wynik2 %d\n",wynik);
    return 0;
}

2
Are the numbers in order?
In this Kata, your function receives an array of integers as input. Your task is to determine whether the numbers are in ascending order. An array is said to be in ascending order if there are no two adjacent integers where the left integer exceeds the right integer in value.

For the purposes of this Kata, you may assume that all inputs are valid, i.e. non-empty arrays containing only integers.

Note that an array of 1 integer is automatically considered to be sorted in ascending order since all (zero) adjacent pairs of integers satisfy the condition that the left integer does not exceed the right integer in value. An empty list is considered a degenerate case and therefore will not be tested in this Kata - feel free to raise an Issue if you see such a list being tested.

For example:

in_asc_order({1,2,4,7,19}, 5); // returns true
in_asc_order({1,2,3,4,5}, 5); // returns true
in_asc_order({1,6,10,18,2,4,20}, 7); // returns false

in_asc_order({9,8,7,6,5,4,3,2,1}, 9); // returns false because the numbers are in DESCENDING order

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

    const int arr0[] = { 1,2,4,7,19 };
    const int arr1[] = { 1,2,3,4,5 };
    const int arr2[] = { 1,6,10,18,2,4,20 };
    const int arr3[] = { 9,8,7,6,5,4,3,2,1 };

bool in_asc_order(const int *arr, size_t arr_size) {

bool wyn=true;

if (arr_size==1)
    return true;

arr_size-=1;

for (int i=0;i<arr_size;i++)
  {
  if(arr[i+1]<=arr[i])
    wyn=false;
  }
  return wyn;
}


int main()
{
    printf("wynik1= %d\n",in_asc_order(arr0,5));
    printf("wynik1= %d\n",in_asc_order(arr1,5));
    printf("wynik1= %d\n",in_asc_order(arr2,7));
    printf("wynik1= %d\n",in_asc_order(arr3,9));

    return 0;
}


3 

Write a function called repeatStr which repeats the given string string exactly n times.

repeatStr(6, "I") // "IIIIII"
repeatStr(5, "Hello") // "HelloHelloHelloHelloHello"


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *repeat_str(size_t count, char *src) {

  int dl=strlen(src);
  dl=count*dl;

  char* nowy=malloc(dl);
  strcpy(nowy,"");

for (int i=0;i<count;i++)
  strcat(nowy,src);

  return nowy;
}


int main(void)
{

    printf("%s\n",repeat_str(5,"ABC"));
    return 0;
}.

4
The number n is Evil if it has an even number of 1's in its binary representation.
The first few Evil numbers: 3, 5, 6, 9, 10, 12, 15, 17, 18, 20

The number n is Odious if it has an odd number of 1's in its binary representation.
The first few Odious numbers: 1, 2, 4, 7, 8, 11, 13, 14, 16, 19


You have to write a function that determine if a number is Evil of Odious, function should return "It's Evil!" in case of evil number and "It's Odious!" in case of odious number.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

const char *evil(int value)
{
  unsigned int count = 0;
  while (value)
  {
    count += value & 1;
    value >>= 1;
  }

    if (count%2)
        return "It's Odious!";
    else
        return "It's Evil!";
}

int main(void)
{
    printf("%s\n",evil(1));
    printf("%s\n",evil(2));
    printf("%s\n",evil(3));
    printf("%s\n",evil(8));
    printf("%s\n",evil(14));
    return 0;
}


5 Sum of Triangular Numbers
Your task is to return the sum of Triangular Numbers up-to-and-including the nth Triangular Number.

Triangular Number: "any of the series of numbers (1, 3, 6, 10, 15, etc.) obtained by continued summation of the natural numbers 1, 2, 3, 4, 5, etc."

[01]
02 [03]
04 05 [06]
07 08 09 [10]
11 12 13 14 [15]
16 17 18 19 20 [21]
e.g. If 4 is given: 1 + 3 + 6 + 10 = 20.

Triangular Numbers cannot be negative so return 0 if a negative number is given.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int sumTriangularNumbers(int n)
{
  if (n<=0)
    return 0;

int temp[n];

for (int i=0;i<n;i++)
    temp[i]=i+1;

int parzyste=n%2 ? 1:0;

int m=n;
m>>=1;
int sum=temp[m]+temp[m-1];

sum=sum*(m+parzyste);

int sum2=sum;

m=n;
for (int i=1;i<n;i++)
{
    sum=sum-m;
    m=m-1;
    sum2=sum2+sum;
}
  return sum2;
}



int main(void)
{
    //printf("%d\n",sumTriangularNumbers(1));
    //printf("%d\n",sumTriangularNumbers(2));
    //printf("%d\n",sumTriangularNumbers(3));
    //printf("%d\n",sumTriangularNumbers(4));
    printf("%d\n",sumTriangularNumbers(4));
    //printf("%d\n",sumTriangularNumbers(6));
    //printf("%d\n",sumTriangularNumbers(7));
    return 0;
}


6 Count all the sheep on farm in the heights of New Zealand
Every week (Friday and Saturday night), the farmer and his son count amount of sheep returned to the yard of their farm.

They count sheep on Friday night, the same goes for Saturday (suppose that sheep returned on Friday are not feeding back on hills on Saturday).

As sheep are not coming in one flock, you will be given two arrays (one for each night) representing number of sheep as they were returning to the yard during the evenings (entries are positive ints, higher than zero).

Farmer and his son know the total amount of their sheep, you will be given this number as third parameter.

Your goal is to calculate the amount of sheep lost (not returned) to the farm after Saturday night counting.

Example 1: Input: {1, 2}, {3, 4}, 15 --> Output: 5

Example 2: Input: {3, 1, 2}, {4, 5}, 21 --> Output: 6


#include<stdio.h>
#include<string.h>

// friday and saturday are 0-terminated arrays, i.e. the last element (and only that) will be 0.
int lostSheep(const int *friday, const int* saturday, int total)
{
    int sum=0;
    while(*friday)
    {
        sum=sum+*friday;
        friday++;
    }

    while(*saturday)
    {
        sum=sum+*saturday;
        saturday++;
    }
    return total-sum;
}

int friday1[]={1,2,0};
int saturday1[]={3,4,0};
int friday2[]={3,1,2,0};
int saturday2[]={3,4,0};


int main(void)
{
printf("%d\n",lostSheep(friday1,saturday1,15));
printf("%d\n",lostSheep(friday2,saturday2,21));
    return 0;
}


7 Exclamation marks series #4: Remove all exclamation marks from sentence but ensure a exclamation mark at the end of string

Remove all exclamation marks from sentence but ensure a exclamation mark at the end of string. For a beginner kata, you can assume that the input data is always a non empty string, no need to verify it.

Examples
remove("Hi!") === "Hi!"
remove("Hi!!!") === "Hi!"
remove("!Hi") === "Hi!"
remove("!Hi!") === "Hi!"
remove("Hi! Hi!") === "Hi Hi!"
remove("Hi") === "Hi!"


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stddef.h>

char* remove_bang(char* s)
{
    int dl=strlen(s);
    char *wynik = malloc(dl+1);
    int j=0;
    for (int i=0;i<dl;i++)
    {

        if (s[i]==33)
            wynik[j]='\0';
        else
        {
            wynik[j++]=s[i];
            wynik[j]='\0';
        }
    }
    strcat(wynik,"!");
    return wynik;
}


int main(void)
{

printf("%s\n",remove_bang("Hi!"));
printf("%s\n",remove_bang("Hi!!!"));
printf("%s\n",remove_bang("!Hi"));
printf("%s\n",remove_bang("!Hi!"));
printf("%s\n",remove_bang("Hi! Hi!"));
printf("%s\n",remove_bang("Hi"));
    return 0;
}

8 Sum without highest and lowest number

Sum all the numbers of the array (in F# and Haskell you get a list) except the highest and the lowest element (the value, not the index!).
(The highest/lowest element is respectively only one element at each edge, even if there are more than one with the same value!)

Example:

{ 6, 2, 1, 8, 10 } => 16
{ 1, 1, 11, 2, 3 } => 6


If array is empty, null or None, or if only 1 Element exists, return 0.
Note:In C++ instead null an empty vector is used. In C there is no null. ;-)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stddef.h>

int tab1[]={6,2,1,8,10};
int tab2[]={1,1,11,2,3};

int sum(int* numbers, int numbersCount)
{
  if (numbersCount==1)
    return 0;

  int temp;
  int zamiana=1;

  while(zamiana)
    {
    zamiana=0;
    for(int i=0;i<numbersCount-1;i++)
        {
        if (numbers[i]>numbers[i+1])
            {
            temp=numbers[i];
            numbers[i]=numbers[i+1];
            numbers[i+1]=temp;
            zamiana=1;
            }
        }
     }

temp=0;
for (int i=1;i<numbersCount-1;i++)
temp+=numbers[i];

return temp;

}


int main(void)
{
printf("%d\n",sum(tab1,5));
printf("%d\n",sum(tab2,5));


    return 0;
}


9 Powers of i
i is the imaginary unit, it is defined by i² = -1, therefore it is a solution to x²+1=0.

Your Task
is to write a function pofi that returns i to the power of a given non-negative integer in its simplest form as a string (answer may contain i).

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stddef.h>

char * pofi(unsigned n) {
  static const char *solution[] = {"1","i","-1","-i"};
  return solution[n % 4];
}

int main()
{
printf("%s\n",pofi(0));
printf("%s\n",pofi(1));
printf("%s\n",pofi(2));
printf("%s\n",pofi(3));
printf("%s\n",pofi(4));
return 0;
}


10 Partial Word Searching
Write a method that will search an array of strings for all strings that contain another string, (bez ego!! ignoring capitalization.)  Then return an array of the found strings.

The method takes two parameters, the query string and the array of strings to search, and returns an array.

If the string isn't contained in any of the strings in the array, the method returns an array containing a single string: "Empty" (or Nothing in Haskell, or "None" in Python and C)

Examples
If the string to search for is "me", and the array to search is ["home", "milk", "Mercury", "fish"], the method should return ["home", "Mercury"].

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>

size_t ResultCount=1;

const char *string[] = { "home", "milk", "mercury", "fish", "melania" };
const char *quer = "me";



char ** word_search(const char *query,const char **strings,size_t count, size_t *pResultCount)
{

char **tablica=malloc(count);
*pResultCount=0;

for(int i=0;i<(int)count;i++)
    {
    if(strstr(strings[i],query))
        {
        tablica[*pResultCount]=malloc(strlen(strings[i])+1);
        strcpy(tablica[*pResultCount],strings[i]);
        (*pResultCount)++;
        }
    }
    return tablica;
}



int main()
{

for (int i=0;i<ResultCount;i++)
 printf("%s \n", word_search(quer, string, sizeof(string)/sizeof(string[0]),&ResultCount)[i] );


    return 0;
}