기본 콘텐츠로 건너뛰기

10월, 2018의 게시물 표시

5.4 Arrays task Calling In an Array Context

  <Script>  "use strict"; let arr = ["a", "b"]; arr.push( function () {   alert(this); }) //it pushes an object which is function at arr[2] //but why does the function contains a and b? because of this? //what "this" means? the whole array? is that why? //oh maybe it should arr[2](); //but why line 17 shows even the contents of the function?   </Script>

Sorting - Insertion

#include <stdio.h> #include <stdlib.h> #define INDEX 11 int main(){ const int index = 10;   int arr[INDEX] = {88, 55, 24, 1, 6, 35, 78, 56, 100, 11, 1}; for(int i = 0; i < INDEX; i++){ //traverse unsorted part int temp = arr[i]; //compare with the latest sorted element int j = i - 1; // j = sorted indexs //temp = element to put in sorted part //repeat till sorted part index is smaller or same as i while(temp < arr[j] && j >= 0){ //first j + 1 means arr[i] //while excution means couldnt find right place //thus sorted element part should move one cells to right side arr[j + 1] = arr[j]; //reverse traversal j--; } //when finally j + 1 is right place //while finished -> should ++ to point proper j //or no while excution -> it is in order => stay where u r arr[j+1] = temp; } printf("The sorted array is:\n"); for(int i = 0; i < 11;

5.3 task Extract Money

  <Script>  "use strict"; //dollorsign iin hexa = "\u0024" function extractCurrencyValue (str){   let target = "\u0024";   let pos = -1;   while ( (pos = str.indexOf(target, pos + 1)) != -1 ){   //  alert(pos);     if(pos == 0){       return (+str.slice(1));     }     else {       return (+str.substring(str.length - 1, 0) );     }   } } alert(extractCurrencyValue('120$') === 120);   </Script>

5.3 task Truncate The Text

<!DOCTYPE html> <html> <body>   <Script>  "use strict"; function truncate (str, maxlength){   if(str.length >= maxlength){     alert(str.substring(0,maxlength) + "...");   }   else alert(str); }   truncate("What I'd like to tell on this topic is:", 20);   truncate("Hi everyone!", 20);   </Script> </body> </html>

God damn Star Drawing

import java.util.Scanner ; public class ex2 { public static void main (String[] arg){ Scanner in = new Scanner(System. in ) ; int input = in.nextInt() ; for ( int i = 0 ; i < input ; i++){ for ( int j = 0 ; j <= i ; j++){ System. out .print( "*" ) ; } System. out .println( "" ) ; } for ( int i = input - 1 ; i> 0 ; i--){ // runs n-1 times for ( int j = 0 ; j < i ; j++){ System. out .print( "*" ) ; } System. out .print( " \n " ) ; } } } //이젠 멈춰주세요..제발....

4.4 task Calculator

4.4 task <script> "use strict" let calculator = { // input1 : 0, // input2 : 0, //do not need new value definition //such an amazing language it is...!!! read() { this.input1 = +prompt('Input first operand', ); this.input2 = +prompt("Input second operand", ); /////////////to make the input to be INTEGER WE NEED THIS + }, sum(){ return this.input1 + this.input2; }, mul(){ return this.input1 * this.input2; }, }; calculator.read(); alert(calculator.sum() ); alert(calculator.mul() ); </script>

4.4 task Chained Function

4.4 task chain <script> "use strict" let ladder = { step: 0, up() { this.step++; return this; }, down() { this.step--; return this; }, showStep: function() { // shows the current step alert( this.step ); } }; ladder.up().up().down().showStep(); </script>

5.3 task - Blocking spam

<!DOCTYPE html> <html> <body>   <Script> function checkSpam(str){   str = str.toLowerCase();   let tar1 = "xxx";   let tar2 = "viagra";   let pos = -1;   while( (pos = str.indexOf(tar1, pos + 1) != -1) ){     //condition means when it found     return true;   }   pos = -1;   while( (pos = str.indexOf(tar2, pos + 1) != -1) ){     //condition means when it found     return true;   } return false; }  alert(checkSpam('innocent Rabbit') );   </Script> </body> </html>

Sorting - Selection

#include <stdio.h> //writer 5396952 Hyeonmin LEE #include <stdlib.h> #define INDEX 11 int main(){ const int index = 10;   int arr[INDEX] = {88, 55, 24, 1, 6, 35, 78, 56, 100, 11, 1}; int small; int temp; for(int n = 0; n < 11; n++){ small = n; for(int i = n; i < 11; i++){ if(arr[small] > arr[i]){ small = i; } } temp = arr[n]; arr[n] = arr[small]; arr[small] = temp; } printf("The sorted array is:\n"); for(int i = 0; i < 11; i++){ printf("%d", arr[i]); if(i != 10){ printf(" "); } else printf("\n"); } return 0; }

Sorting - Buble

#include <stdio.h> //writer 5396952 Hyeonmin LEE #include <stdlib.h> #define INDEX 11 int main(){ const int index = 10;   int arr[INDEX] = {88, 55, 24, 1, 6, 35, 78, 56, 100, 11, 1}; int temp; for(int i = 0; i < 9; i++){ for (int j = 0; j < 10; j++) { while(arr[j] > arr[j+1]){ temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } printf("The sorted array is:\n"); for(int i = 0; i < 11; i++){ printf("%d", arr[i]); if(i != 10){ printf(" "); } else printf("\n"); } return 0; }

Heap

#include <stdio.h> #include <stdlib.h> #include <math.h> #include <stdbool.h> #define MAXSIZE 10 typedef struct { int heapAry[MAXSIZE]; int last; int size; int maxSize; }HEAP; HEAP* heapCreate (int maxSize){ HEAP *heap = (HEAP*)malloc (sizeof (HEAP)); if(!heap) return NULL; heap->last = -1; //the index (works as top in stack) heap->size = 0; //wtf? heap->maxSize = (int) pow(2, ceil(log2(maxSize))) - 1; //force heap size to power of 2-1 int array[maxSize]; //allocate the array[maxSize] times size of void* return heap; } void reheapUp(HEAP* heap, int childLoc){ int parent = 0; // start a traverse from the parent int hold = 0; //if child is root node, right goes to return if(childLoc){ //if the child is not null == not root node parent = (childLoc - 1) / 2; if(heap->heapAry[childLoc] > heap->heapAry[parent]){ //if compare returns 1 or true, //it means child is bigger than par