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