Last active
June 13, 2024 07:42
-
-
Save wonkwh/f9503e9f10e1a8076257ba228050532e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// dabb(dart apprentice beyond the basic> ch1 Challenges | |
void main() { | |
basicStringManipulationExercise1(); | |
basicStringManipulationExercise2(); | |
buildingStringsExercise(); | |
stringValidationExercise(); | |
challenge1(); | |
challenge2(); | |
} | |
/// Basic String Manipulation: Exercise 1 | |
/// | |
/// 아래 텍스트와 같이 여러 줄의 문자열을 한줄로 | |
/// | |
/// ``` | |
/// France | |
/// USA | |
/// Germany | |
/// Benin | |
/// China | |
/// 한국 | |
/// Mexico | |
/// Mongolia | |
/// ``` | |
void basicStringManipulationExercise1() { | |
const countriesString = ''' | |
France | |
USA | |
Germany | |
Benin | |
한국 | |
China | |
Mexico | |
Mongolia'''; | |
final countriesList = countriesString.split('\n'); | |
print(countriesList); | |
} | |
/// Basic String Manipulation: Exercise 2 | |
/// | |
/// 다음 텍스트에서 ':]`를 이모티콘으로 변경 | |
/// | |
/// ``` | |
/// How's the Dart book going? :] | |
/// ``` | |
void basicStringManipulationExercise2() { | |
const original = "How's the Dart book going? :]"; | |
final replaced = original.replaceAll(':]', '😊'); | |
print(replaced); | |
} | |
/// Building Strings Exercise | |
/// | |
/// Use a string buffer | |
/// | |
/// ```text | |
/// ********* | |
/// * ******** | |
/// ** ******* | |
/// *** ****** | |
/// **** ***** | |
/// ***** **** | |
/// ****** *** | |
/// ******* ** | |
/// ******** * | |
/// ********* | |
/// ``` | |
/// | |
void buildingStringsExercise() { | |
final buffer = StringBuffer(); | |
for (int i = 0; i < 10; i++) { | |
for (int j = 0; j < 10; j++) { | |
if (j == i) { | |
buffer.write(' '); | |
} else { | |
buffer.write('*'); | |
} | |
} | |
buffer.write('\n'); | |
} | |
print(buffer); | |
} | |
/// String Validation Exercise | |
/// | |
/// credit card number contains only numbers 16 digits long. | |
void stringValidationExercise() { | |
final regex = RegExp(r'^[0-9]{16}$'); | |
print(regex.hasMatch('1111222233334444')); // true | |
print(regex.hasMatch('123')); // false | |
print(regex.hasMatch('aaaabbbbccccdddd')); // false | |
print(regex.hasMatch('12341234123412345')); // false | |
} | |
/// Challenge 1: Email Validation | |
/// | |
/// Write a regular expression to validate an email address. | |
void challenge1() { | |
print('Challenge 1'); | |
// ^: start | |
// \w+: alphanumeric characters | |
// @: literal @ | |
// \.: literal . | |
// $: end | |
final regex = RegExp(r'^\w+@\w+\.\w+$'); | |
print(regex.hasMatch('[email protected]')); // true | |
print(regex.hasMatch('[email protected]')); // true | |
print(regex.hasMatch('my [email protected]')); // false | |
print(regex.hasMatch('meexample.com')); // false | |
print(regex.hasMatch('me@examplecom')); // false | |
} | |
/// Challenge 2: Karaoke Words | |
/// | |
/// LRC 파일에 노래가사와 timestamps 포함되어 있다. | |
/// 시간과 가사를 추출 | |
/// | |
/// ``` | |
/// [00:12.34]Row, row, row your boat | |
/// ``` | |
/// | |
/// 다음 형식으로 출력 | |
/// | |
/// ```text | |
/// minutes: 00 | |
/// seconds: 12 | |
/// hundredths: 34 | |
/// lyrics: Row, row, row your boat | |
/// ``` | |
/// | |
/// Solve the problem twice, once with `substring` and once with regex groups. | |
void challenge2() { | |
print('Challenge 2'); | |
usingSubstring(); | |
usingRegexGroups(); | |
} | |
void usingSubstring() { | |
const line = '[00:12.34]Row, row, row your boat'; | |
final minutes = line.substring(1, 3); | |
final seconds = line.substring(4, 6); | |
final hundredths = line.substring(7, 9); | |
final lyrics = line.substring(10); | |
print('minutes: $minutes'); | |
print('seconds: $seconds'); | |
print('hundredths: $hundredths'); | |
print('lyrics: $lyrics'); | |
} | |
void usingRegexGroups() { | |
const line = '[00:12.34]Row, row, row your boat'; | |
// ^ start | |
// \[ literal [ | |
// (\d+) group of digits | |
// : literal : | |
// \. literal . | |
// \] literal ] | |
// (.+) group of characters (lyrics) | |
// $ end | |
final regex = RegExp(r'^\[(\d+):(\d+)\.(\d+)\](.+)$'); | |
final match = regex.firstMatch(line); | |
final minutes = match?.group(1); | |
final seconds = match?.group(2); | |
final hundredths = match?.group(3); | |
final lyrics = match?.group(4); | |
print('minutes: $minutes'); | |
print('seconds: $seconds'); | |
print('hundredths: $hundredths'); | |
print('lyrics: $lyrics'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment