2018/07/08

[c] Mine Sweep

#include 
#include 
#include 
#include 

#define debug_

char mine[100][100];
int nmax = 0, mmax = 0;

int getMin(int a, int b){ 
    if(a>b)   
        return b;
    else    
        return a;
}

int getMax(int a, int b){ 
    if(a>b)
        return a;
    else
        return b;
}

int getCnt(int x, int y){ 
    int i = 0, j = 0, cnt = 0;

    for(i=getMax(x-1, 0);i<=getMin(x+1, nmax);i++){
        for(j=getMax(y-1, 0);j<=getMin(y+1, mmax);j++){
            if(mine[i][j] == '*'){
                cnt ++; 
            }   
#ifdef debug
            printf("---- getCnt : i:%d, j:%d, cnt:%d\n", i, j, cnt); 
#endif
        }   
    }   
    return cnt;
}

int main(){
    char buf[101];
    int s, i, j, n, m;

    s = 1; /* Set counter */
    n = 0; /* Row counter in set */
    m = 0; /* Column counter in row */
    while(gets(buf)){

        if(strlen(buf) < 1)
            break;

        if(n == 0){ 
            nmax = buf[0] - '0';
            mmax = buf[2] - '0';
#ifdef debug
            printf("---- nmax : %d, mmax : %d\n", nmax, mmax);
#endif
            if(nmax == 0 && mmax == 0)
                break;
        }
        else{
            for(j=0;j < mmax;j++){
                mine[n-1][j] = buf[j];
#ifdef debug
                printf("---- n : %d, m : %d, char : %c\n", n, j, mine[n-1][j]);
#endif
            }
        }

        if(n == nmax){
            printf("Field #%d\n", s);
            for(i=0;i < nmax;i++){
                for(j=0;j< mmax;j++){
                    if(mine[i][j] == '*')
                        printf("%c",'*');
                    else
                        printf("%d",getCnt(i, j));

                    if(j == (mmax - 1))
                        printf("\n");
                }
            }
            s++;
            n = 0;
            memset(mine,'\0',sizeof(mine));
        }
        else{
            n++;
        }
    }

    return 0;
}

[c] Stack


#include 
#include 
#define MAXSIZE 100 

typedef struct stack{
    int stk[MAXSIZE];
    int top;
} STACK;


void Push(int x, STACK *s){
    int num;
    s->top ++; 
    printf("Push %d\n", s->top);
    s->stk[s->top] = x;
    return;
}

int Pop(STACK *s){
    int num;
    printf("Pop %d\n", s->top);
    num = s->stk[s->top];
    s->top --; 
    return num;
}

void Init(STACK *s){
    s->top = -1; 
    printf("Init %d\n", s->top);
    return;
}

int main(){

    STACK *s = (STACK *)malloc(sizeof(STACK));

    Init(s);

    Push(1, s); 
    Push(2, s); 
    Push(3, s); 

    printf("%d\n", Pop(s));
    printf("%d\n", Pop(s));
    printf("%d\n", Pop(s));

}

2018/07/07

[c] learn-c.org


Welcome

Welcome to the learn-c.org free interactive C tutorial.
Whether you are an experienced programmer or not, this website is intended for everyone who wishes to learn the C programming language.
There is no need to download anything - Just click on the chapter you wish to begin from, and follow the instructions. Good luck!


http://www.learn-c.org

2018/06/12

[python] text from url

import requests
import json
url = 'http://www.quotesondesign.com/wp-json/posts'
resp = requests.get(url)

# srting to Python data type
c_lst = json.loads(resp.text)

for txt in c_lst:
    # unpacking
    for key, value in txt.items():
        print(key, ' : ', value)

2018/06/11

[python] json

import json

# Json String to variable
sample = {
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

# string to Json encoding with formatting
sample_json = json.dumps(sample, indent = 4)
print(sample_json)
print('')

# Decoding Json String to Python type
sample_structured_json = json.loads(sample_json)
print(sample_structured_json)
print('')

print(sample_structured_json['glossary']['title'])
print('')

for i in sample_structured_json['glossary']['GlossDiv']['GlossList']['GlossEntry']:
    print(i)