inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

[인프런 워밍업 클럽 CS 3기] 1주차 미션

이승녕
0

운영체제


1.

    while(true){
      wait(1); // 1초 멈춤
      bool isActivated = checkSkillActivated(); // 체크
    }

위 코드는 1초 마다 플레이어가 스킬을 사용했는지 체크하는 코드입니다. 이 방식은 폴링방식입니다. 1초마다 체크하기 때문에 성능에 좋지 않습니다. 이를 해결하기 위한 방식으로 어떤 걸 이용해야 할까요?

 

2. 프로그램과 프로세스가 어떻게 다른가요?

 

3. 멀티프로그래밍과 멀티프로세싱이 어떻게 다른가요?

 

4.운영체제는 프로세스를 관리하기 위해서 어떤 것을 사용하나요?

 

5. 컨텍스트 스위칭이란 뭔가요?

 


 

자료구조와 알고리즘


1. 여러분은 교실의 학생 정보를 저장하고 열람할 수 있는 관리 프로그램을 개발하려고 합니다.

이 때 여러분이라면 학생의 정보를 저장하기 위한 자료구조를 어떤 걸 선택하실 건가요? 이유를 함께 적어주세요.

 

2. 여러분은 고객의 주문을 받는 프로그램을 개발하려고 합니다. 주문은 들어온 순서대로 처리됩니다. 이 때 여러분이라면 어떤 자료구조를 선택하실 건가요? 이유를 함께 적어주세요.

 

3. 우리가 구현한 스택은 0번 인덱스, 즉 입구쪽으로 데이터가 삽입되고 나오는 구조입니다. 반대로 마지막 인덱스, 즉 출구쪽으로 데이터가 삽입되고 나오는 구조로 코드를 변경해주세요.

 using warmingup_CS.LinkedList;

namespace warmingup_CS.Stack;

public class StackTest
{
	private LinkedListTest _linkedList;

	public void Push(object data)
	{
		_linkedList.InsertAt(0, data);
	}

	public object Pop()
	{
		try
		{
			return _linkedList.DeleteAt(0);
		}
		catch (Exception e)
		{
			return null;
		}
	}

	public object Peek()
	{
		return _linkedList.GetNodeAt(0);
	}

	public bool IsEmpty()
	{
		return _linkedList.Count == 0;
	}
}
using warmingup_CS.LinkedList;

namespace warmingup_CS.Stack;

public class StackTest
{
	private LinkedListTest _linkedList;

	public void Push(object data)
	{
		_linkedList.InsertLast(data);
	}

	public object Pop()
	{
		try
		{
			return _linkedList.DeleteLast();
		}
		catch (Exception e)
		{
			return null;
		}
	}

	public object Peek()
	{
		return _linkedList.GetNodeAt(this._linkedList.Count - 1);
	}

	public bool IsEmpty()
	{
		return _linkedList.Count == 0;
	}
}

 

4. 해시테이블의 성능은 해시 함수에 따라 달라집니다. 수업 시간에 등번호를 이용해 간단한 해시 함수를 만들어봤습니다. 이번엔 등번호가 아닌 이름을 이용해 데이터를 골고루 분산시키는 코드로 수정해주세요.

힌트: charCodeAt() 함수를 이용

예시: name1 = "이운재"; name1.charCodeAt(0); // 51060 이운재의 0번 인덱스 ‘이’의 유니코드 출력

public class HashTableTest
{
	private DoublyLinkedListTest[] _array;

	public HashTableTest()
	{
		_array = new DoublyLinkedListTest[10];
		for (int i = 0; i < 10; i++)
		{
			_array[i] = new DoublyLinkedListTest();
		}
	}

	public int HashFunction(int number)
	{
		return number % 10;
	}

	public void Set(int key, object value)
	{
		_array[HashFunction(key)].InsertAt(0, new HashTableData(key, value));
	}

	public object Get(int key)
	{
		var current = _array[HashFunction(key)].Head;
		while (current != null)
		{
			if ((int)((HashTableData)current.Data).Key == key)
			{
				return ((HashTableData)current.Data).Value;
			}
			current = current.Next;
		}
		return "null";
	}

	public object Remove(int key)
	{
		var list = _array[HashFunction(key)];
		var current = list.Head;
		int deleteIndex = 0;
		while (current != null)
		{
			if ((int)((HashTableData)current.Data).Key == key)
			{
				return list.DeleteAt(deleteIndex).ToString();
			}
			current = current.Next;
			deleteIndex++;
		}
		return "null";
	}

	//문자열 Key에 대한 해시 함수
	public int HashFunction(string str)
	{
		int sum = 0;
		for (int i = 0; i < str.Length; i++)
		{
			sum += str[i];
		}
		return sum % 10; // 해시 값 계산
	}
	public void Set(string key, object value)
	{
		_array[HashFunction(key)].InsertAt(0, new HashTableData(key, value));
		Console.WriteLine($"{key}, {HashFunction(key)}");
	}

	public object Get(string key)
	{
		var current = _array[HashFunction(key)].Head;
		while (current != null)
		{
			if (((HashTableData)current.Data).Key.ToString() == key)
			{
				return ((HashTableData)current.Data).Value;
			}
			current = current.Next;
		}
		return "null";
	}

	public object Remove(string key)
	{
		var list = _array[HashFunction(key)];
		var current = list.Head;
		int deleteIndex = 0;
		while (current != null)
		{
			if (((HashTableData)current.Data).Key.ToString() == key)
			{
				return list.DeleteAt(deleteIndex).ToString();
			}
			current = current.Next;
			deleteIndex++;
		}
		return "null";
	}
}

public class HashTableData
{
	private object _key;
	public object Key => _key;
	private object _value;
	public object Value => _value;

	public HashTableData(object key, object value)
	{
		_key = key;
		_value = value;
	}

	public override string ToString()
	{
		return JsonSerializer.Serialize(this);
	}
}

이운재, 4

최진철, 8

홍명보, 2

유상철, 5

송종국, 9

박지성, 4

김남일, 0

이영표, 3

최태욱, 5

설기현, 4

이천수, 8

var hashTable2 = new HashTableTest();
hashTable2.Set("이운재", 1);
hashTable2.Set("최진철", 4);
hashTable2.Set("홍명보", 20);
hashTable2.Set("유상철", 6);
hashTable2.Set("송종국", 22);
hashTable2.Set("박지성", 21);
hashTable2.Set("김남일", 5);
hashTable2.Set("이영표", 10);
hashTable2.Set("최태욱", 8);
hashTable2.Set("설기현", 9);
hashTable2.Set("이천수", 14);

Console.WriteLine("최진철: " + hashTable2.Get("최진철"));
hashTable2.Remove("최진철");
Console.WriteLine("최진철: " + hashTable2.Get("최진철"));
Console.WriteLine("이천수: " + hashTable2.Get("이천수"));

image

 

답변 0