https://www.hackerrank.com/challenges/weather-observation-station-10/problem

 

Weather Observation Station 9 | HackerRank

Query an alphabetically ordered list of CITY names not starting with vowels.

www.hackerrank.com

Query the list of CITY names from STATION that do not end with vowels. Your result cannot contain duplicates.

Input Format

The STATION table is described as follows:

 

a,e,i,o,u 로 끝나지 않는 CITY 찾기

반응형

https://www.hackerrank.com/challenges/weather-observation-station-9/problem

 

Weather Observation Station 6 | HackerRank

Query a list of CITY names beginning with vowels (a, e, i, o, u).

www.hackerrank.com

Query the list of CITY names from STATION that do not start with vowels. Your result cannot contain duplicates.

Input Format

The STATION table is described as follows:

a,e,i,o,u 로 시작하지 않는 CITY를 찾는 문제 

반응형

https://www.hackerrank.com/challenges/weather-observation-station-8/problem

 

Weather Observation Station 8 | HackerRank

Query CITY names that start AND end with vowels.

www.hackerrank.com

Query the list of CITY names from STATION which have vowels (i.e., a, e, i, o, and u) as both their first and last characters. Your result cannot contain duplicates.

Input Format

The STATION table is described as follows:

 

STATION 테이블에서 a,e,i,o,u로 시작하고 a,e,i,o,u로 끝나는 CITY를 찾는 문제

반응형

https://www.hackerrank.com/challenges/weather-observation-station-7/problem

 

Weather Observation Station 7 | HackerRank

Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION.

www.hackerrank.com

Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION. Your result cannot contain duplicates.

Input Format

The STATION table is described as follows:

a,e,i,o,u 로 끝나는 CITY 중복없이 찾는 문제 

마찬가지로 OR로 조건을 나누거나 정규표현식으로 해결

반응형

https://www.hackerrank.com/challenges/weather-observation-station-6/problem

 

Weather Observation Station 6 | HackerRank

Query a list of CITY names beginning with vowels (a, e, i, o, u).

www.hackerrank.com

Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from STATION. Your result cannot contain duplicates.

Input Format

The STATION table is described as follows:

a,e,i,o,u 로 시작하는 CITY를 중복없이 찾는 문제

OR로 조건을 추가하거나 정규표현식으로 해결가능 

반응형

https://www.hackerrank.com/challenges/weather-observation-station-4/problem

 

Weather Observation Station 4 | HackerRank

Find the number of duplicate CITY names in STATION.

www.hackerrank.com

 

Find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table.  The STATION table is described as follows:

 

전체 CITY 수와 겹치지 않는 CITY 수를 빼는 문제

 

반응형

이동 평균 필터란?

->지정된 n개의 데이터만 가지고 평균을 구하는 방식

->새로운 데이터가 들어오면 가장 오래된 데이터는 버리고 n개로 평균을 구하는 방식

 

이번에도 굳이 구조체를 만들 필요는 없지만 구조체로 만들어보았다. 

만약 평균을 구할 데이터의 갯수 n이 정해져 있지 않고 사용자가 마음대로 초기화 할 수 있다고 가정하여, 

n이 정해지면 해당 갯수만큼 데이터를 담을 배열은 malloc을 사용하였다. 

#include <stdio.h>
#include <stdlib.h>
typedef struct 
{
    int cnt;
    int num;
    int *data;
    float preAvg;
    float nowAvg;
    int idx;
}mvAvg;

int init_mvAvgFilter(mvAvg *mvAvg, int num){
    mvAvg->cnt=0;
    mvAvg->num=num;
    mvAvg->data =(int*)malloc(sizeof(int)*num);
    mvAvg->preAvg=0;
    mvAvg->idx=0;
}

float mvAvgFilter(mvAvg *mvAvg,int data){
    if (mvAvg->cnt < mvAvg->num){
        mvAvg->data[mvAvg->cnt]=data;
        mvAvg->cnt++;
        
        int i=0;
        int sum=0;
        for (i=0;i<mvAvg->cnt;i++){
            sum+=mvAvg->data[i];
        }
        mvAvg->nowAvg=(float)sum/mvAvg->cnt;
        mvAvg->preAvg=mvAvg->nowAvg;
        return mvAvg->nowAvg;
    }

    mvAvg->nowAvg=(data-mvAvg->data[mvAvg->idx])/mvAvg->num+mvAvg->preAvg;
    mvAvg->data[mvAvg->idx]=data;
    mvAvg->idx++;
    if(mvAvg->idx == mvAvg->num){
        mvAvg->idx=0;
    }
    mvAvg->preAvg=mvAvg->nowAvg;
    return mvAvg->nowAvg;
}

int main(){
    mvAvg mvAvg;
    init_mvAvgFilter(&mvAvg,4);
    int i=0;
    for (i=1;i<=10;i++){
        printf("%d : %f\r\n",i,mvAvgFilter(&mvAvg,i));
    }
}

잘 되는 것 같은데.. 테스트가 부족하다.. 

 

반응형

'지식저장소 > C' 카테고리의 다른 글

[C] AverageFilter (평균 필터)  (0) 2022.04.28
[C] Ring Buffer / 링버퍼 구현  (0) 2022.03.29
[C/BOJ] 9012 괄호 - Stack이용  (0) 2022.02.16
[C]구조체를 이용한 Stack 구현  (0) 2022.02.16
[C/STM32] Uart로 printf 대신하기  (0) 2022.01.14

데이터들이 계속 들어올 때마다 평균을 갱신하는 AverageFilter 소스 

 

데이터의 수와 이전 평균만 전역 변수나 static 변수로 저장해도 되지만

구조체로 만들어보았다.

#include <stdio.h>

typedef struct 
{
    int dataCnt;
    float preAvg;
    float nowAvg;
}Avg;

float avgFilter(Avg *Avg,int data){
    if(Avg->dataCnt==0){
        Avg->dataCnt++;
        Avg->nowAvg=data;
        Avg->preAvg=data;
        return Avg->nowAvg;
    }
    Avg->preAvg=Avg->nowAvg;
    Avg->dataCnt++;
    Avg->nowAvg=(1 - (float)1/(Avg->dataCnt))*(Avg->preAvg)+((float)1/Avg->dataCnt)*data;
    return Avg->nowAvg;
}

int main(){
    Avg avg;
    avg.dataCnt = 0;
    avg.nowAvg = 0;
    avg.preAvg = 0;

    printf("%f\r\n", avgFilter(&avg,10));
    printf("%f\r\n", avgFilter(&avg,20));
    printf("%f\r\n", avgFilter(&avg,30));
    printf("%f\r\n", avgFilter(&avg,40));
    printf("%f\r\n", avgFilter(&avg,50));
    printf("%f\r\n", avgFilter(&avg,60));
    printf("%f %f\r\n",avg.preAvg,avg.nowAvg);
}

반응형

+ Recent posts