동구의_C# & Unity_개발일지
2024.03.14 내일배움캠프 55일차 TIL_Unity (최종 프로젝트, 알고리즘, 기술 면접) 본문
알고리즘 코드카타 39일차
시저 암호
문제 설명
어떤 문장의 각 알파벳을 일정한 거리만큼 밀어서 다른 알파벳으로 바꾸는 암호화 방식을 시저 암호라고 합니다. 예를 들어 "AB"는 1만큼 밀면 "BC"가 되고, 3만큼 밀면 "DE"가 됩니다. "z"는 1만큼 밀면 "a"가 됩니다. 문자열 s와 거리 n을 입력받아 s를 n만큼 민 암호문을 만드는 함수, solution을 완성해 보세요.
using System;
public class Solution {
public string solution(string s, int n) {
string answer = "";
foreach(char c in s)
{
if(c != ' ')
{
int tmp = 0;
if((int) c < 91)
{
tmp = (int) c + n;
if(tmp > 90) tmp = 64 + (tmp - 90);
}
else
{
tmp = (int) c + n;
if(tmp > 122) tmp = 96 + (tmp - 122);
}
answer += Convert.ToChar(tmp);
}
else
answer += ' ';
}
return answer;
}
}
기술면접 연습하기 7일차
가비지 컬렉션이란 무엇인지 설명해주세요.
가비지 컬렉션(Garbage Collection)은 프로그래밍에서 메모리 관리를 위해 사용되는 기술이다.
프로그램이 실행되는 동안 메모리를 할당하고 사용하는데, 이 과정에서 더 이상 필요하지 않은 메모리 영역이 발생할 수 있다. 이를 해제하지 않으면 메모리 누수(memory leak)가 발생하여 시스템 성능에 문제를 일으킬 수 있다.
가비지 컬렉션은 이러한 메모리 누수를 방지하기 위해 사용된다.
최종 프로젝트
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.VisualScripting;
using UnityEngine;
public class QuestDataManager
{
public static readonly QuestDataManager instance = new QuestDataManager();
//private Dictionary<int, ShopData> dicShopData;
private Dictionary<int, ItemData> dicItemData;
private Dictionary<int, QuestData> dicQuestData;
private Dictionary<int, RewardItemData> dicRewardItemData;
//test
private Dictionary<string, Dictionary<int, RawData>> dic = new Dictionary<string, Dictionary<int, RawData>>();
private Dictionary<string, string> pathDic = new Dictionary<string, string>();
private QuestDataManager()
{
pathDic.Add(typeof(QuestData).ToString(), "quest_data");
pathDic.Add(typeof(RewardItemData).ToString(), "reward_item_data");
}
//public ShopData GetShopData(int id)
//{
// return this.dicShopData[id];
//}
//public void Loadshopdata()
//{
// TextAsset asset = Resources.Load<TextAsset>("Data/shop_data");
// var json = asset.text;
// Debug.Log(json);
// ShopData[] arrShopDatas = JsonConvert.DeserializeObject<short[]>(json);
// this.dicShopData = arrShopDatas.ToDictionary(x => x.id);
// Debug.LogFormat("shop data loaded : {0}", this.dicShopData.Count);
//}
//public List<ShopData> GetShopDatas()
//{
// return this.dicShopData.Values.ToList();
//}
public void LoadItemData()
{
TextAsset asset = Resources.Load<TextAsset>("Data/item_data");
string json = asset.text;
Debug.Log(json);
ItemData[] arr = JsonConvert.DeserializeObject<ItemData[]>(json);
//this.dicItemData = arr.ToDictionary(x => x.id);
Debug.Log("item data loaded.");
Debug.LogFormat("item data count: <color=yellow>{0}</color>", this.dicItemData.Count);
}
public ItemData GetItemDAta(int id)
{
if (this.dicItemData.ContainsKey(id))
{
return this.dicItemData[id];
}
Debug.LogFormat("key ({0}) not found.", id);
return null;
}
//public rewarditemdata getrandomitemdata()
//{
// var randid = random.range(0, this.dicitemdata.count) + 100;
// return this.getitemdata(randid);
//}
public void LoadQuestData()
{
TextAsset asset = Resources.Load<TextAsset>("Data/Quest_data");
string json = asset.text;
QuestData[] arr = JsonConvert.DeserializeObject<QuestData[]>(json);
this.dicQuestData = arr.ToDictionary(x => x.id);
Debug.LogFormat("quest data loaded : <color=yellow>{0}</color?", this.dicQuestData.Count);
}
public QuestData GetQuestData(int id)
{
return this.dicQuestData[id];
}
public void LoadRewardItemData()
{
TextAsset asset = Resources.Load<TextAsset>("Data/reward_item_data");
string json = asset.text;
RewardItemData[] arr = JsonConvert.DeserializeObject<RewardItemData[]>(json);
this.dicRewardItemData = arr.ToDictionary(x => x.id);
Debug.LogFormat("reward item data loaded : <color=yellow>{0}</color>", this.dicRewardItemData.Count);
}
public RewardItemData GetRewardItemData(int id)
{
return this.dicRewardItemData[id];
}
public void LoadData<T>() where T : RawData
{
Debug.LogFormat("LoadData: {0}",typeof(T).ToString());
var Key = typeof(T).ToString();
var path = this.pathDic[Key];
TextAsset asset = Resources.Load<TextAsset>(string.Format("Data/{0}", path));
string json = asset.text;
T[] arr = JsonConvert.DeserializeObject<T[]>(json);
var a = arr.ToDictionary(x => x.id, x => (RawData) x);
if (!dic.ContainsKey(Key))
{
this.dic.Add(Key, a);
}
Debug.LogFormat("key: {0}", Key);
Debug.LogFormat("{0} loaded : <color=yellow>{1}</color>", path, this.dic[Key].Count);
}
public Dictionary<int, T> GetDataDic<T>() where T : RawData
{
var Key = typeof(T).ToString();
var a = this.dic[Key];
return a.ToDictionary(x => x.Key, x => (T)x.Value);
}
}
퀘스트 로직과 Reward 아이템을 구현하였다.