<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>ad-astra.log</title>
        <link>https://velog.io/</link>
        <description>Hello, Rabbit</description>
        <lastBuildDate>Wed, 06 Oct 2021 12:57:33 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <image>
            <title>ad-astra.log</title>
            <url>https://images.velog.io/images/ad-astra/profile/881adb89-cb01-4f75-b10b-cae769acac40/social.png</url>
            <link>https://velog.io/</link>
        </image>
        <copyright>Copyright (C) 2019. ad-astra.log. All rights reserved.</copyright>
        <atom:link href="https://v2.velog.io/rss/ad-astra" rel="self" type="application/rss+xml"/>
        <item>
            <title><![CDATA[Algorithm 27 - Total amount of points]]></title>
            <link>https://velog.io/@ad-astra/Algorithm-27-Total-amount-of-points</link>
            <guid>https://velog.io/@ad-astra/Algorithm-27-Total-amount-of-points</guid>
            <pubDate>Wed, 06 Oct 2021 12:57:33 GMT</pubDate>
            <description><![CDATA[<h1 id="q">Q.</h1>
<p>Description:
Our football team finished the championship. The result of each match look like &quot;x:y&quot;. Results of all matches are recorded in the collection.</p>
<p>For example: [&quot;3:1&quot;, &quot;2:2&quot;, &quot;0:1&quot;, ...]</p>
<p>Write a function that takes such collection and counts the points of our team in the championship. Rules for counting points for each match:</p>
<p>if x&gt;y - 3 points
if x&lt;y - 0 point
if x=y - 1 point
Notes:</p>
<p>there are 10 matches in the championship
0 &lt;= x &lt;= 4
0 &lt;= y &lt;= 4</p>
<h1 id="a">A)</h1>
<p>```c
int points(const char* const games[10]) 
{
  int i = 0, point = 0;;
  while (i &lt; 10)
  {
    if (games[i][0] &gt; games[i][2])
      point += 3;
    else if (games[i][0] &lt; games[i][2])
      point += 0;
    else if (games[i][0] == games[i][2])
      point += 1;
    i++;
  }
  return point;
}</p>
<p>another solution
#include &lt;stdlib.h&gt;</p>
<p>int points(char* games[]) {
  int p = 0;
  for (int i = 0; i &lt; 10; ++i) {
    int x = games[i][0] - &#39;0&#39;;
    int y = games[i][2] - &#39;0&#39;;
    p += (x &gt; y) * 3 + (x == y);
  }
  return p;
}</p>
<p>another solution
#include &lt;stdlib.h&gt;</p>
<p>int points(char* games[]) {</p>
<pre><code>int points = 0; 
int x, y;

for (int i = 0; i &lt; 10; i++)
{
  sscanf(games[i], &quot;%d:%d&quot;, &amp;x, &amp;y);

  if (x &gt; y) points += 3;
  else if (x == y) points += 1;
}

return points;</code></pre><p>} -&gt; sscanf() 로 받아서 x, y에 저장할 수 있는 듯.
원리는 배열에 들어갈때 input stream을 열어주는 듯?</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[Algorithm 26 - Sum without highest and lowest number]]></title>
            <link>https://velog.io/@ad-astra/Algorithm-26-Sum-without-highest-and-lowest-number</link>
            <guid>https://velog.io/@ad-astra/Algorithm-26-Sum-without-highest-and-lowest-number</guid>
            <pubDate>Wed, 06 Oct 2021 12:42:55 GMT</pubDate>
            <description><![CDATA[<h1 id="q">Q.</h1>
<p>Description:
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!)</p>
<p>Example:</p>
<p>{ 6, 2, 1, 8, 10 } =&gt; 16
{ 1, 1, 11, 2, 3 } =&gt; 6</p>
<p>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. ;-)</p>
<p>-- There&#39;s no null in Haskell, therefore Maybe [Int] is used. Nothing represents null.
Have fun coding it and please don&#39;t forget to vote and rank this kata! :-)</p>
<p>I have created other katas. Have a look if you like coding and challenges.</p>
<h1 id="a">A)</h1>
<p>```c
int sum(int* numbers, int numbersCount)
{
  int mid_sum = 0;
  int min_idx = 0;
  int max_idx = 0;
  for (int i = 1; i &lt; numbersCount; i++)
  {
    if (numbers[min_idx] &gt; numbers[i])
      min_idx = i;
  }
  for (int i = 1; i &lt; numbersCount; i++)
  {
    if (numbers[max_idx] &lt; numbers[i])
      max_idx = i;
  }
  for (int i = 0; i &lt; numbersCount; i++)
  {
    if (i != min_idx &amp; i != max_idx)
      mid_sum += numbers[i];
  }<br>  return mid_sum;
}</p>
<p>another solution
남들은 for 문 하나로 모든 원소 sum 하면서 max랑 min 찾아내고 나중에 max, min을 sum에서 뻄.</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[Algorithm 25 - Fake Binary]]></title>
            <link>https://velog.io/@ad-astra/Algorithm-25-Fake-Binary</link>
            <guid>https://velog.io/@ad-astra/Algorithm-25-Fake-Binary</guid>
            <pubDate>Wed, 06 Oct 2021 12:33:12 GMT</pubDate>
            <description><![CDATA[<h1 id="q">Q.</h1>
<p>Description:
Given a string of digits, you should replace any digit below 5 with &#39;0&#39; and any digit 5 and above with &#39;1&#39;. Return the resulting string.</p>
<h1 id="a">A)</h1>
<p>```c
#include &lt;string.h&gt;</p>
<p>void fakeBin(const char *digits, char *buffer) 
{
//  for (size_t i = 0; i &lt; strlen(digits); i++)
//  {
//    if (digits[i] &gt;= &#39;0&#39; &amp;&amp; digits[i] &lt; &#39;5&#39;)
//      buffer[i] = &#39;0&#39;;
//    else if (digits[i] &gt;= &#39;5&#39; &amp;&amp; digits[i] &lt;= &#39;9&#39;)
//      buffer[i] = &#39;1&#39;;
//  }
//  buffer[strlen(buffer)] = &#39;\0&#39;;
  size_t len = strlen(digits);
  for (size_t i = 0; i &lt; len; i++)
    buffer[i] = (digits[i] &gt;= &#39;5&#39; ? &#39;1&#39; : &#39;0&#39;);
  buffer[len] = &#39;\0&#39;;
} -&gt; 주석코드는 왜 안됐지? -&gt; buffer len을 재니까 안됐지 ㅋㅋ</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[Algorithm 24 - Beginner Series #2 Clock]]></title>
            <link>https://velog.io/@ad-astra/Algorithm-24-Beginner-Series-2-Clock</link>
            <guid>https://velog.io/@ad-astra/Algorithm-24-Beginner-Series-2-Clock</guid>
            <pubDate>Wed, 06 Oct 2021 10:48:36 GMT</pubDate>
            <description><![CDATA[<h1 id="q">Q.</h1>
<p>Description:
Clock shows h hours, m minutes and s seconds after midnight.</p>
<p>Your task is to write a function which returns the time since midnight in milliseconds.</p>
<p>Example:
h = 0
m = 1
s = 1</p>
<p>result = 61000
Input constraints:</p>
<p>0 &lt;= h &lt;= 23
0 &lt;= m &lt;= 59
0 &lt;= s &lt;= 59</p>
<h1 id="a">A)</h1>
<p>```c
int past(int h, int m, int s) 
{
  int result;
  return result = h * 3600000 + m * 60000 + s * 1000;
}</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[Algorithm 23 - Convert a String to a Number!]]></title>
            <link>https://velog.io/@ad-astra/Algorithm-23-Convert-a-String-to-a-Number</link>
            <guid>https://velog.io/@ad-astra/Algorithm-23-Convert-a-String-to-a-Number</guid>
            <pubDate>Wed, 06 Oct 2021 10:43:44 GMT</pubDate>
            <description><![CDATA[<h1 id="q">Q.</h1>
<p>Description:
Note: This kata is inspired by Convert a Number to a String!. Try that one too.</p>
<p>Description
We need a function that can transform a string into a number. What ways of achieving this do you know?</p>
<p>Note: Don&#39;t worry, all inputs will be strings, and every string is a perfectly valid representation of an integral number.</p>
<p>Examples
&quot;1234&quot; --&gt; 1234
&quot;605&quot;  --&gt; 605
&quot;1405&quot; --&gt; 1405
&quot;-7&quot; --&gt; -7</p>
<h1 id="a">A)</h1>
<p>```c
int string_to_number(const char *src) 
{
  return atoi(src);
}</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[Algorithm 22 - Function 1 - hello world]]></title>
            <link>https://velog.io/@ad-astra/Algorithm-22-Function-1-hello-world</link>
            <guid>https://velog.io/@ad-astra/Algorithm-22-Function-1-hello-world</guid>
            <pubDate>Wed, 06 Oct 2021 10:40:05 GMT</pubDate>
            <description><![CDATA[<h1 id="q">Q.</h1>
<p>Description:
Description:
Make a simple function called greet that returns the most-famous &quot;hello world!&quot;.</p>
<p>Style Points
Sure, this is about as easy as it gets. But how clever can you be to create the most creative hello world you can think of? What is a &quot;hello world&quot; solution you would want to show your friends?</p>
<h1 id="a">A)</h1>
<p>```c
//Write a function <code>greet</code> that returns &quot;hello world!&quot;
char *greet(void)
{
  char *ptr = &quot;hello world!&quot;;
  return ptr;
}</p>
<p>another solution
#include &lt;string.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;</p>
<p>char *greet()
{
  char *code = &quot;++++++++++[&gt;++++++++++&gt;+++++++++++&gt;++++++++++++&gt;+++&lt;&lt;&lt;&lt;-]&gt;++++.---.&gt;--..+++.&gt;&gt;++.&lt;-.&lt;.&gt;-----.&lt;---.&lt;-.&gt;&gt;&gt;+.&quot;;</p>
<p>  char byteArr[] = {0x00, 0x00, 0x00, 0x00, 0x00};
  int pointer = 0;
  int outPointer = 0;
  char out[14] = &quot;&quot;;
    int length = (int)strlen(code);
  for (int i = 0; i &lt; length; i++)
  {
    char chr = code[i];
    switch (chr)
    {
    case &#39;&gt;&#39;:
      pointer++;
      break;</p>
<pre><code>case &#39;&lt;&#39;:
  pointer--;
  break;

case &#39;+&#39;:
  byteArr[pointer]++;
  break;

case &#39;-&#39;:
  byteArr[pointer]--;
  break;

case &#39;.&#39;:
  out[outPointer] = byteArr[pointer];
  outPointer++;
  break;

case &#39;[&#39;:
  if (byteArr[pointer] == 0x00)
    for (int j = 0; j &lt; length; j++)
      if (code[j] == &#39;]&#39;)
        i = j;
  break;

case &#39;]&#39;:
  if (byteArr[pointer] != 0x00)
    for (int j = 0; j &lt; length; j++)
      if (code[j] == &#39;[&#39;)
        i = j;
  break;
}</code></pre><p>  }</p>
<p>  char* result = malloc(strlen(out) + 1);</p>
<pre><code>strcpy(result, out);

return result;</code></pre><p>} -&gt; ??</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[Algorithm 21 - Get the mean of an array]]></title>
            <link>https://velog.io/@ad-astra/Algorithm-21-Get-the-mean-of-an-array</link>
            <guid>https://velog.io/@ad-astra/Algorithm-21-Get-the-mean-of-an-array</guid>
            <pubDate>Wed, 06 Oct 2021 10:35:13 GMT</pubDate>
            <description><![CDATA[<h1 id="q">Q.</h1>
<p>Description:
It&#39;s the academic year&#39;s end, fateful moment of your school report. The averages must be calculated. All the students come to you and entreat you to calculate their average for them. Easy ! You just need to write a script.</p>
<p>Return the average of the given array rounded down to its nearest integer.</p>
<p>The array will never be empty.</p>
<h1 id="a">A)</h1>
<p>```c
#include &lt;stddef.h&gt;</p>
<p>int get_average(const int *marks, size_t count)
{
  int sum = 0;
  for (size_t i = 0; i &lt; count; i++)
    sum += marks[i];
  return sum / count;
}</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[Algorithm 20 - Invert values]]></title>
            <link>https://velog.io/@ad-astra/Algorithm-20-Invert-values</link>
            <guid>https://velog.io/@ad-astra/Algorithm-20-Invert-values</guid>
            <pubDate>Wed, 06 Oct 2021 10:32:05 GMT</pubDate>
            <description><![CDATA[<h1 id="q">Q.</h1>
<p>Description:
Given a set of numbers, return the additive inverse of each. Each positive becomes negatives, and the negatives become positives.</p>
<p>invert([1,2,3,4,5]) == [-1,-2,-3,-4,-5]
invert([1,-2,3,-4,5]) == [-1,2,-3,4,-5]
invert([]) == []
Notes:
All values are greater than INT_MIN
The input should be modified, not returned.</p>
<h1 id="a">A)</h1>
<p>```c
#include &lt;stddef.h&gt;</p>
<p>void invert(int *values, size_t values_size)
{
  for (size_t i = 0; i &lt; values_size; i++)
    values[i] *= -1;
}</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[Algorithm 19 - Beginner - Lost Without a Map]]></title>
            <link>https://velog.io/@ad-astra/Algorithm-19-Beginner-Lost-Without-a-Map</link>
            <guid>https://velog.io/@ad-astra/Algorithm-19-Beginner-Lost-Without-a-Map</guid>
            <pubDate>Wed, 06 Oct 2021 10:29:01 GMT</pubDate>
            <description><![CDATA[<h1 id="q">Q.</h1>
<p>Description:
Given an array of integers, return a new array with each value doubled.</p>
<p>For example:</p>
<p>[1, 2, 3] --&gt; [2, 4, 6]</p>
<h1 id="a">A)</h1>
<p>```c
#include &lt;stddef.h&gt;</p>
<p>// return a <em>new, dynamically allocated</em> array with each element doubled.
int <em>maps(const int *arr, size_t size)
{
  int *tmp = (int</em>)malloc(sizeof(int) * size);
  if (!tmp)
    return NULL;
  for (size_t i = 0; i &lt; size; i++)
    tmp[i] = arr[i] * 2;
  return tmp;
}</p>
<p>another solution
int <em>maps(const int *arr, size_t size) {
  int</em> a = (int*)malloc(size * sizeof(int));
  for (int i = 0; i &lt; size; a[i++] = arr[i] * 2);
  return a;
} -&gt; 이런것도 되넹</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[Algorithm 18 - Count of positives / sum of negatives]]></title>
            <link>https://velog.io/@ad-astra/Algorithm-18-Count-of-positives-sum-of-negatives</link>
            <guid>https://velog.io/@ad-astra/Algorithm-18-Count-of-positives-sum-of-negatives</guid>
            <pubDate>Wed, 06 Oct 2021 10:24:49 GMT</pubDate>
            <description><![CDATA[<h1 id="q">Q.</h1>
<p>Description:
Given an array of integers.</p>
<p>Return an array, where the first element is the count of positives numbers and the second element is sum of negative numbers.</p>
<p>If the input array is empty or null, return an empty array.</p>
<p>Example
For input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15], you should return [10, -65].</p>
<h1 id="a">A)</h1>
<p>```c
#include &lt;stddef.h&gt;</p>
<p>void count_positives_sum_negatives(
  int *values, size_t count, int *positivesCount, int *negativesSum) 
{
  for (size_t i = 0; i &lt; count; i++)
  {
    if (values[i] &gt; 0)
      *positivesCount = *positivesCount + 1;
    else if (values[i] &lt; 0)
      *negativesSum += values[i];
  }
}  -&gt; 0 is not positive</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[Algorithm 17 - A Needle in the Haystack]]></title>
            <link>https://velog.io/@ad-astra/Algorithm-17-A-Needle-in-the-Haystack</link>
            <guid>https://velog.io/@ad-astra/Algorithm-17-A-Needle-in-the-Haystack</guid>
            <pubDate>Wed, 06 Oct 2021 10:11:36 GMT</pubDate>
            <description><![CDATA[<h1 id="q">Q.</h1>
<p>Description:
Can you find the needle in the haystack?</p>
<p>Write a function findNeedle() that takes an array full of junk but containing one &quot;needle&quot;</p>
<p>After your function finds the needle it should return a message (as a string) that says:</p>
<p>&quot;found the needle at position &quot; plus the index it found the needle, so:</p>
<p>find_needle([&#39;hay&#39;, &#39;junk&#39;, &#39;hay&#39;, &#39;hay&#39;, &#39;moreJunk&#39;, &#39;needle&#39;, &#39;randomJunk&#39;])
should return &quot;found the needle at position 5&quot;</p>
<h1 id="a">A)</h1>
<p>```c
#include &lt;stddef.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;</p>
<p>char <em>find_needle(const char *</em>haystack, size_t count)
{
  char *buf = calloc(100, sizeof(char));
  char *tmp = malloc(256);
  if (!buf || !tmp)
    return NULL;
  size_t i = 0;
  strcpy(buf, &quot;found the needle at position &quot;);
  while (i &lt; count)
  {
    if (strcmp(haystack[i], &quot;needle&quot;) == 0)
      break;
    i++;
  }
  sprintf(tmp, &quot;%d&quot;, i);
  strcat(buf, tmp);
  return buf;
}</p>
<p>another solution
#include &lt;stdio.h&gt;</p>
<p>char <em>find_needle(const char **haystack, size_t count)
{
  for(int i=0; i&lt;count;++i)
  {
    if(!strcmp(haystack[i], &quot;needle&quot;)) // strcmp will return 0 if true, so we need &#39;!&#39; to it to work
    {
      char</em> buff;
      asprintf(&amp;buff, &quot;found the needle at position %d&quot;, i);
      return buff;
    }
  }
} -&gt; asprintf() .. i의 숫자를 넣은 문자열(read only)을 buff에 저장.</p>
<p>another solution
#include &lt;stddef.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;</p>
<p>char <em>find_needle(const char *</em>haystack, size_t count)
{
  while (strcmp(haystack[--count], &quot;needle&quot;));
  char *buf = malloc(128);
  sprintf(buf, &quot;found the needle at position %d&quot;, count);
  return buf;
} -&gt; sprintf() .. 위와 비슷.</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[Algorithm 16 - Basic Mathematical Operations]]></title>
            <link>https://velog.io/@ad-astra/Algorithm-16-Basic-Mathematical-Operations</link>
            <guid>https://velog.io/@ad-astra/Algorithm-16-Basic-Mathematical-Operations</guid>
            <pubDate>Wed, 06 Oct 2021 09:48:22 GMT</pubDate>
            <description><![CDATA[<h1 id="q">Q.</h1>
<p>Description:
Your task is to create a function that does four basic mathematical operations.</p>
<p>The function should take three arguments - operation(string/char), value1(number), value2(number).
The function should return result of numbers after applying the chosen operation.</p>
<p>Examples
basicOp(&#39;+&#39;, 4, 7)         // Output: 11
basicOp(&#39;-&#39;, 15, 18)       // Output: -3
basicOp(&#39;*&#39;, 5, 5)         // Output: 25
basicOp(&#39;/&#39;, 49, 7)        // Output: 7</p>
<h1 id="a">A)</h1>
<p>```c
int basic_op(char op, int value1, int value2) 
{
  int cul = 0;
  if (op == &#39;+&#39;)
    cul = value1 + value2;
  else if (op == &#39;-&#39;)
    cul = value1 - value2;
  else if (op == &#39;*&#39;)
    cul = value1 * value2;
  else if (op == &#39;/&#39;)
    cul = value1 / value2;
  return cul;
}</p>
<p>another solution
int basic_op(char op, int x, int y) 
{
  return op == 43 ? x + y : op == 45 ? x - y : op == 42 ? x * y : x / y;
} -&gt; false 면 다음 기호로 넘어가는 방식. 숫자는 아스키코드로 연산기호들.</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[Algorithm 15 - Is n divisible by x and y?]]></title>
            <link>https://velog.io/@ad-astra/Algorithm-15-Is-n-divisible-by-x-and-y</link>
            <guid>https://velog.io/@ad-astra/Algorithm-15-Is-n-divisible-by-x-and-y</guid>
            <pubDate>Wed, 06 Oct 2021 09:42:26 GMT</pubDate>
            <description><![CDATA[<h1 id="q">Q.</h1>
<p>Description:
Create a function that checks if a number n is divisible by two numbers x AND y. All inputs are positive, non-zero digits.</p>
<p>Examples:</p>
<p>1) n =   3, x = 1, y = 3 =&gt;  true because   3 is divisible by 1 and 3
2) n =  12, x = 2, y = 6 =&gt;  true because  12 is divisible by 2 and 6
3) n = 100, x = 5, y = 3 =&gt; false because 100 is not divisible by 3
4) n =  12, x = 7, y = 5 =&gt; false because  12 is neither divisible by 7 nor 5</p>
<h1 id="a">A)</h1>
<p>```c
#include &lt;stdbool.h&gt;</p>
<p>bool isDivisible(int n, int x, int y) 
{
  return ((n % x == 0 &amp;&amp; n % y == 0) ? true : false);
}</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[Algorithm 14 - Keep Hydrated!]]></title>
            <link>https://velog.io/@ad-astra/Algorithm-14-Keep-Hydrated</link>
            <guid>https://velog.io/@ad-astra/Algorithm-14-Keep-Hydrated</guid>
            <pubDate>Wed, 06 Oct 2021 09:36:37 GMT</pubDate>
            <description><![CDATA[<h1 id="q">Q.</h1>
<p>Description:
Nathan loves cycling.</p>
<p>Because Nathan knows it is important to stay hydrated, he drinks 0.5 litres of water per hour of cycling.</p>
<p>You get given the time in hours and you need to return the number of litres Nathan will drink, rounded to the smallest value.</p>
<p>For example:</p>
<p>time = 3 ----&gt; litres = 1</p>
<p>time = 6.7---&gt; litres = 3</p>
<p>time = 11.8--&gt; litres = 5</p>
<h1 id="a">A)</h1>
<p>```c
int Liters(double time) 
{
  int drink = 0;
  int save = (int)time;
  drink = save / 2;
  return drink;
}</p>
<p>another solution
int Liters(double time) {
  return (int)(time * 0.5);
}</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[Algorithm 13 - Square(n) Sum]]></title>
            <link>https://velog.io/@ad-astra/Algorithm-13-Squaren-Sum</link>
            <guid>https://velog.io/@ad-astra/Algorithm-13-Squaren-Sum</guid>
            <pubDate>Wed, 06 Oct 2021 09:31:14 GMT</pubDate>
            <description><![CDATA[<h1 id="q">Q.</h1>
<p>Description:
Complete the square sum function so that it squares each number passed into it and then sums the results together.</p>
<p>For example, for [1, 2, 2] it should return 9 because 1^2 + 2^2 + 2^2 = 9.</p>
<h1 id="a">A)</h1>
<p>```c
#include &lt;stddef.h&gt;</p>
<p>int square_sum(const int *values, size_t count)
{
  int sum = 0;
  for (; count; count--)
    sum += values[count - 1] * values[count - 1];
  return sum;
}</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[Algorithm 12 - Counting sheep...]]></title>
            <link>https://velog.io/@ad-astra/Algorithm-12-Counting-sheep</link>
            <guid>https://velog.io/@ad-astra/Algorithm-12-Counting-sheep</guid>
            <pubDate>Wed, 06 Oct 2021 09:26:25 GMT</pubDate>
            <description><![CDATA[<h1 id="q">Q.</h1>
<p>Description:
Consider an array/list of sheep where some sheep may be missing from their place. We need a function that counts the number of sheep present in the array (true means present).</p>
<p>For example,</p>
<p>{ true,  true,  true,  false,
  true,  true,  true,  true,
  true,  false, true,  false,
  true,  false, false, true,
  true,  true,  true,  true,
  false, false, true,  true }
The correct answer would be 17.</p>
<p>Hint: Don&#39;t forget to check for bad values like null/undefined</p>
<h1 id="a">A.</h1>
<p>```c
#include &lt;stdbool.h&gt;
#include &lt;stddef.h&gt;</p>
<p>size_t count_sheep(const bool *sheep, size_t count)
{
  size_t find = 0;
  while (count--)
  {
    if (sheep[count])
      find++;
  }
  return find;
}</p>
<p>another solution
size_t count_sheep(const bool *sheep, size_t count) {
  int n=0;
  while(count--) n += sheep[count];
  return n;
} -&gt; true는 1, false는 0이기 때문에 가능한 식.</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[Algorithm 11 - Convert a Number to a String!]]></title>
            <link>https://velog.io/@ad-astra/Algorithm-11-Convert-a-Number-to-a-String</link>
            <guid>https://velog.io/@ad-astra/Algorithm-11-Convert-a-Number-to-a-String</guid>
            <pubDate>Wed, 06 Oct 2021 09:16:06 GMT</pubDate>
            <description><![CDATA[<h1 id="q">Q.</h1>
<p>Description:
We need a function that can transform a number into a string.</p>
<p>What ways of achieving this do you know?</p>
<p>In C, return a dynamically allocated string that will be freed by the test suite.</p>
<p>Examples:
123 --&gt; &quot;123&quot;
999 --&gt; &quot;999&quot;</p>
<h1 id="a">A)</h1>
<p>```c
#include &lt;stdlib.h&gt;</p>
<p>const char* number_to_string(int number)
{
  char *ptr;
  int len = 0;
  long long nb = (long long)number;</p>
<p>  if (number &lt; 0)
    nb *= -1;</p>
<p>  while (nb != 0)
  {
    nb = nb / 10;
    len++;
  }</p>
<p>  if (number &gt; 0)
    ptr = (char<em>)malloc(sizeof(char) * len + 1);
  else
  {
    len++;
    ptr = (char</em>)malloc(sizeof(char) * len + 1);
  }</p>
<p>  if (!ptr)
    return NULL;</p>
<p>  ptr[len] = &#39;\0&#39;;</p>
<p>  nb = (long long)number;</p>
<p>  if (number &lt; 0)
    nb *= -1;</p>
<p>  while (len--)
  {
    ptr[len] = &#39;0&#39; + (nb % 10);
    nb /= 10;
  }</p>
<p>  if (number &lt; 0)
    ptr[0] = &#39;-&#39;;</p>
<p>  return ptr;
}</p>
<p>another solution
#include &lt;stdio.h&gt;</p>
<p>const char* number_to_string(int number) {
    char *s;
    asprintf(&amp;s, &quot;%d&quot;, number);
    return s;
}</p>
<p>another solution
#include &lt;stdlib.h&gt;</p>
<p>const char* number_to_string(int number) {
    char *itoa = malloc(256);
    sprintf(itoa, &quot;%d&quot;, number);
    return itoa;
} -&gt; sprintf() function으로 첫 매개변수에 받는 버퍼에 number를 문자열로 저장하는 듯.</p>
<p>another solution
#include &lt;stdlib.h&gt;</p>
<p>const char* number_to_string(int number) {</p>
<pre><code>size_t bufferLength = snprintf(NULL, 0, &quot;%d&quot;, number); //Find the length of the string conversion
char* buffer = malloc(bufferLength + 1); //dynamically allocate the string

snprintf(buffer, bufferLength + 1, &quot;%d&quot;, number); //populate the string

return buffer; //return the string</code></pre><p>}</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[Algorithm 10 - Find the smallest integer in the array]]></title>
            <link>https://velog.io/@ad-astra/Algorithm-10-Find-the-smallest-integer-in-the-array</link>
            <guid>https://velog.io/@ad-astra/Algorithm-10-Find-the-smallest-integer-in-the-array</guid>
            <pubDate>Wed, 06 Oct 2021 08:39:42 GMT</pubDate>
            <description><![CDATA[<h1 id="q">Q.</h1>
<p>Description:
Given an array of integers your solution should find the smallest integer.</p>
<p>For example:</p>
<p>Given [34, 15, 88, 2] your solution will return 2
Given [34, -345, -1, 100] your solution will return -345
You can assume, for the purpose of this kata, that the supplied array will not be empty.</p>
<h1 id="a">A)</h1>
<p>```c
#include &lt;stddef.h&gt;</p>
<p>int find_smallest_int(int *vec, size_t len)
{
  size_t min_idx = 0;
  size_t i = 1;
  while (i &lt; len)
  {
    if (vec[min_idx] &gt; vec[i])
        min_idx = i;
    i++;
  }
  return vec[min_idx];
}</p>
<p>another solution
사망연산자 쓰기</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[Algorithm 9 - Remove String Spaces]]></title>
            <link>https://velog.io/@ad-astra/Algorithm-9-Remove-String-Spaces</link>
            <guid>https://velog.io/@ad-astra/Algorithm-9-Remove-String-Spaces</guid>
            <pubDate>Wed, 06 Oct 2021 08:24:58 GMT</pubDate>
            <description><![CDATA[<h1 id="q">Q.</h1>
<p>Description:
Simple, remove the spaces from the string, then return the resultant string.</p>
<p>For C, you must return a new dynamically allocated string.</p>
<h1 id="a">A)</h1>
<p>```c
#include &lt;stdlib.h&gt;</p>
<p>char <em>no_space(const char *str_in)
{
  char *ptr;
  int i = 0, j = 0, len = 0;
  while (str_in[i])
  {
    if (str_in[i] != &#39; &#39;)
      len++;
    i++;
  }
  if (!(ptr = (char</em>)malloc(sizeof(char) * len + 1)))
    return NULL;
  i = 0;
  while (str_in[i])
  {
    if (str_in[i] != &#39; &#39;)
      ptr[j++] = str_in[i];
    i++;
  }
  ptr[len] = &#39;\0&#39;;
  return ptr;
}</p>
<p>another solution
char <em>no_space(char *s) {
  char *res = strdup(s), *q = res;
  for (; *s; s++) 
      if (</em>s != &#39; &#39;) 
        *q++ = *s;
  return *q = 0, res;
} -&gt; strdup() function : 문자열 동적할당해주고 카피해주는데 이렇게 하면 직접 쓸 메모리공간보다 더 할당해주기 때문에 낭비될 수 있음.</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[Algorithm 8 - Grasshopper - Summation]]></title>
            <link>https://velog.io/@ad-astra/Algorithm-8-Grasshopper-Summation</link>
            <guid>https://velog.io/@ad-astra/Algorithm-8-Grasshopper-Summation</guid>
            <pubDate>Wed, 06 Oct 2021 07:59:49 GMT</pubDate>
            <description><![CDATA[<h1 id="q">Q.</h1>
<p>Description:
Summation
Write a program that finds the summation of every number from 1 to num. The number will always be a positive integer greater than 0.</p>
<p>For example:</p>
<p>summation(2) -&gt; 3
1 + 2</p>
<p>summation(8) -&gt; 36
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8</p>
<h1 id="a">A)</h1>
<p>```c
int summation(int num) 
{
  int sum = 0;
  for (int i = 1; i &lt;= num; i++)
    sum += i;
  return sum;
}</p>
<p>another solution
int summation(int num) {
  return num * (num + 1) / 2;
} -&gt; ex: num = 10 / (10 * 11) / 2 = 55 / 가우스 공식.</p>
]]></description>
        </item>
    </channel>
</rss>