Easy Coding Problems
Array Challenge
Have the function ArrayChallenge(strArr) read the array of strings stored in strArr which will contain 2 elements: the first element will represent a list of comma-separated numbers sorted in ascending order, the second element will represent a second list of comma-separated numbers (also sorted). Your goal is to return a comma-separated string containing the numbers that occur in elements of strArr in sorted order. If there is no intersection, return the string false.
Once your function is working, take the final output string and concatenate it with your ChallengeToken, and then replace every fourth character with an underscore.
Your ChallengeToken: w2gkyfca1e7
Examples
Input: string {"1, 3, 4, 7, 13", "1, 2, 4, 13, 15"}
Output: 1,4,13
Final Output: 1,4,13w_gky_ca1_7
package main
import (
"fmt"
"strconv"
"strings"
)
// ArrayChallenge menerima slice string berisi 2 elemen (dua daftar angka terurut terpisah koma).
// Tujuannya adalah mencari irisan dari dua daftar tersebut, memproses hasilnya dengan ChallengeToken,
// lalu mengembalikan string yang telah dimodifikasi.
func ArrayChallenge(strArr []string) string {
// Pastikan input memiliki 2 elemen
if len(strArr) != 2 {
return "Input tidak valid" // Atau tangani error sesuai kebutuhan
}
// ChallengeToken
challengeToken := "w2gkyfca1e7"
// 1. Uraikan (Parse) String menjadi Slice Angka
// Fungsi pembantu untuk mengurai string terpisah koma menjadi slice int
parseString := func(s string) []int {
parts := strings.Split(s, ",")
var nums []int
for _, part := range parts {
// Hilangkan spasi jika ada (walaupun contoh tidak menunjukkan spasi, ini praktik yang baik)
part = strings.TrimSpace(part)
if n, err := strconv.Atoi(part); err == nil {
nums = append(nums, n)
}
}
return nums
}
nums1 := parseString(strArr[0])
nums2 := parseString(strArr[1])
// 2. Temukan Irisan (Intersection)
// Menggunakan map untuk menyimpan elemen dari daftar pertama untuk pencarian O(1)
set1 := make(map[int]bool)
for _, n := range nums1 {
set1[n] = true
}
// Slice untuk menyimpan hasil irisan
var intersection []int
for _, n := range nums2 {
if set1[n] {
// Karena kedua daftar sudah terurut, hasil intersection juga akan terurut
intersection = append(intersection, n)
// Hapus dari set1 jika kita hanya perlu satu instance (untuk kasus yang tidak mungkin di sini karena input terurut unik)
// Namun, untuk kasus ini, kita biarkan saja karena yang dicari adalah irisan
}
}
// 3. Bentuk String Hasil (Output String)
var finalOutput string
if len(intersection) == 0 {
// Jika tidak ada irisan, kembalikan "false"
finalOutput = "false"
} else {
// Gabungkan hasil irisan menjadi string terpisah koma
var strIntersection []string
for _, n := range intersection {
strIntersection = append(strIntersection, strconv.Itoa(n))
}
finalOutput = strings.Join(strIntersection, ",")
}
// 4. Konkatenasi dengan ChallengeToken dan Modifikasi
// Ambil string sebelum modifikasi (Satu-satunya perbedaan adalah jika outputnya "false" vs angka)
var stringToModify string
if finalOutput == "false" {
stringToModify = "false" + challengeToken
} else {
stringToModify = finalOutput + challengeToken
}
// Lakukan modifikasi: ganti setiap karakter ke-4 dengan underscore
modifiedChars := []rune(stringToModify)
for i := 3; i < len(modifiedChars); i += 4 { // Mulai dari index ke-3 (karakter ke-4)
modifiedChars[i] = '_'
}
return string(modifiedChars)
}
// Fungsi utama untuk pengujian
func main() {
// Contoh 1 dari soal
input1 := []string{"1, 3, 4, 7, 13", "1, 2, 4, 13, 15"}
fmt.Printf("Input 1: %v\n", input1)
fmt.Printf("Output: %s\n", ArrayChallenge(input1))
// Irisan: 1,4,13
// Gabung: 1,4,13w2gkyfca1e7
// Modif: 1,4,_13w_2gk_yfca_1e7 -> Ada sedikit perbedaan dengan contoh soal
// Mari kita cek kembali contoh soal: Output: 1,4,13 -> Final Output: 1,4,_13w_gky_ca1_7
// Jika kita hitung: 1,4,13w2gkyfca1e7
// Indeks: 0123456789012345678
// Posisi ke-4: 3, 7, 11, 15...
// 1,4,1_3w2g_kyfc_a1e7
// Mari kita ikuti output yang diharapkan soal: 1,4,_13w_gky_ca1_7
// Output soal menunjukkan penggantian karakter ke-4 dari hasil *final output string* yang dimodifikasi.
// Hasil Irisan: 1,4,13
// Digabung: 1,4,13w2gkyfca1e7 (17 karakter)
// 1(0) 4(1) ,(2) 1(3) -> Diganti
// 3(4) w(5) 2(6) g(7) -> Diganti
// k(8) y(9) f(10) c(11) -> Diganti
// a(12) 1(13) e(14) 7(15) -> Diganti
// Solusi saya menghasilkan: 1,4,_13w_2gk_yfca_1e7 -> Ini terlihat lebih sesuai dengan instruksi "ganti setiap karakter keempat"
// Mari kita analisis contoh output soal: 1,4,_13w_gky_ca1_7
// 1,4,13w2gkyfca1e7 (Original)
// 1(1) 4(2) ,(3) 1(4) -> "_"
// 3(5) w(6) 2(7) g(8) -> "_"
// k(9) y(10) f(11) c(12) -> "_"
// a(13) 1(14) e(15) 7(16) -> "_"
// Posisi: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Char: 1 , 4 , 1 3 w 2 g k y f c a 1 e 7
// Indeks: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Jika "setiap karakter keempat" berarti indeks i = 3, 7, 11, 15... (indeks berbasis 0):
// 1,4,13w2gkyfca1e7
// ^ ^ ^ ^
// 1,4,1_3w2g_kyfc_a1e7 (Hasil implementasi di atas - **Inilah yang benar mengikuti instruksi**)
// Catatan: Terdapat kemungkinan perbedaan interpretasi antara "karakter keempat" (i+1 % 4 == 0) dan "setiap 4 karakter" (i=3, i+=4). Solusi di atas mengikuti interpretasi i=3, i+=4.
// Contoh 2 dari soal
input2 := []string{"1, 3, 9, 10, 17, 18", "1, 4, 9, 10"}
fmt.Printf("\nInput 2: %v\n", input2)
fmt.Printf("Output: %s\n", ArrayChallenge(input2))
// Irisan: 1,9,10
// Gabung: 1,9,10w2gkyfca1e7 (17 karakter)
// Modif: 1,9,_10w_2gky_fca1_e7
// Contoh 3 (Tidak ada irisan)
input3 := []string{"1, 3, 5", "2, 4, 6"}
fmt.Printf("\nInput 3: %v\n", input3)
fmt.Printf("Output: %s\n", ArrayChallenge(input3))
// Irisan: false
// Gabung: falsew2gkyfca1e7 (15 karakter)
// Modif: fals_w2gk_yfca_1e7
}String Challenge
Have the function StringChallenge(strArr) read the array of strings stored in strArr which will contain two elements: the first element will be a string and the second element will be a number ranging from 1 to 6. The number represents how many rows to print the string on so that it forms a zig-zag pattern. For example: if strArr is ["coderbyte", "3"] then this word will look like the following if you print it in a zig-zag pattern with 3 rows:
c o e b t d y
Your program should return the word formed by combining the characters as you iterate through each row, so for this example your program should return the string creobtdy.
Once your function is working, take the final output string and concatenate it with your ChallengeToken, and then replace every fourth character with an underscore.
Your ChallengeToken: w2gkyfca1e7
Examples
Input: {"cat", "5"}
Output: cat
SQL Challenge
SQL Challenge Your table: maintable_GO19X
In this MySQL challenge, your query should return the vendor information along with the values from the table cb_vendorinformation. You should combine the values of the two tables based on the GroupID column. The final query should only print out the GroupID, CompanyName, and final count of all rows that are grouped into each company name under a column titled Count. The output table should be then sorted by the Count column and then sorted by GroupID so that a higher number appears first.
Your output should look like the following table.
27
Machinx
1
5
WaterBus Enterprise
1
36
Johnson and Sons
2
35
Shipping & Co.
3
6
Alloy LLC
3
40
FireConsulting
5
39
News Corp.
5
Searching Challenge
Have the function SearchingChallenge(str) take the str parameter being passed, which will contain only alphabetic characters and spaces, and return the first non-repeating character. For example: if str is "agettkgaaeee" then your program should return k. The string will always contain at least one character and there will always be at least one non-repeating character.
Once your function is working, take the final output string and concatenate it with your ChallengeToken, and then replace every fourth character with an underscore.
Your ChallengeToken: w2gkyfca1e7
Examples
Input: "abcdef"
Output: a
Final Output: aw2_kyf_a1e_
Arary Challenge
Have the function ArrayChallenge(arr) take the arr parameter being passed which will be an array of non-negative integers and circularly rotate the array starting from the Nth element where N is equal to the first integer in the array. For example: if arr is [2, 3, 4, 1, 6, 10] then your program should rotate the array starting from the 2nd position because the first element in the array is 2. The final array will therefore be [4, 1, 6, 10, 2, 3], and your program should return the new array as a string, so for this example your program would return 4161023. The first element in the array will always be an integer greater than or equal to 0 and less than the size of the array.
Once your function is working, take the final output string and concatenate it with your ChallengeToken, and then replace every fourth character with an underscore.
Your ChallengeToken: w2gkyfca1e7
Examples
Input: int {3,2,1,6}
Output: 6321
Final Output: 632_w2g_yfc_1e7
Last updated