[
{
"objectID": "index.html",
"href": "index.html",
"title": "Codewars Challenge",
"section": "",
"text": "Pengantar\nBuku ini adalah catatan penyelesaian dalam mengerjakan tantangan-tantangan logika dan pemrograman Codewars menggunakan R/Python dan RStudio/Positron.\n\n \n\nHal ini terinspirasi dari kegiatan di YouTube nya Pak Sandhika Galih WPU.",
"crumbs": [
"Pengantar"
]
},
{
"objectID": "cw0001evenodd.html",
"href": "cw0001evenodd.html",
"title": "\n1 Even or Odd\n",
"section": "",
"text": "8Kyu Tantangan #1/366 - 01 Feb 2025\nhttps://www.codewars.com/kata/53da3dbb4a5168369a0000fe\n\n1.1 Instruction\n\nCreate a function that takes an integer as an argument and returns \"Even\" for even numbers or \"Odd\" for odd numbers.\n\n\n1.2 YouTube Video\n\n\n1.3 Solution Code\nSolusi bar-bar\n\neven_or_odd <- function(n) {\n ifelse(abs(n/2 - as.integer(n/2)) == 0.5, \"Odd\", \"Even\")\n}\n\nSolusi simple\n\neven_or_odd <- function(n) {\n # %% operator modulo\n ifelse(n %% 2 == 1, \"Odd\", \"Even\")\n}\n\n\neven_or_odd <- function(n) c(\"Even\", \"Odd\")[n %% 2 + 1]\n\n\n1.4 Test\n\nlibrary(testthat)\ntest_that('even_or_odd(1) returns \"Odd\"', {\n expect_equal(even_or_odd(1), \"Odd\")\n})\n\n## Test passed 🎉\n\ntest_that('even_or_odd(2) returns \"Even\"', {\n expect_equal(even_or_odd(2), \"Even\")\n})\n\n## Test passed 🥳\n\ntest_that('even_or_odd(0) returns \"Even\"', {\n expect_equal(even_or_odd(0), \"Even\")\n})\n\n## Test passed 😸\n\ntest_that('even_or_odd(-1) returns \"Odd\"', {\n expect_equal(even_or_odd(-1), \"Odd\")\n})\n\n## Test passed 🎉\n\ntest_that('even_or_odd(-2) returns \"Even\"', {\n expect_equal(even_or_odd(-2), \"Even\")\n})\n\n## Test passed 🎊\n\n\n\nSupported by\n\n\nStarCore Analytics",
"crumbs": [
"2025",
"1 Even or Odd"
]
},
{
"objectID": "cw0002multiply.html",
"href": "cw0002multiply.html",
"title": "\n2 Multiply\n",
"section": "",
"text": "8Kyu Tantangan #2/366 - 02 Feb 2025\nhttps://www.codewars.com/kata/50654ddff44f800200000004\n\n2.1 Instruction\n\nThis code does not execute properly. Try to figure out why.\n\n\nmul <- function(a, b) {\n a * c # try to figure out why it doesn't work!\n}\n\n\n2.2 YouTube Video\n\n\n2.3 Solution Code\nKarena argumen dari function mul() adalah a dan b sedangkan operasi perkalian di dalamnya menggunakan c yang belum diketahui dan tidak ada objek tersebut.\nSolusi: ganti objek c di dalam function mul() dengan b dari argumen function tersebut.\n\nmul <- function(a, b) {\n a * b \n}\n\n\n2.4 Test\n\nlibrary(testthat)\n\ntest_that(\"test for associativity of multiplication\", {\n a <- runif(1, 0, 10000)\n b <- runif(1, 0, 10000)\n c <- runif(1, 0, 10000)\n expect_equal(mul(a, b), a * b)\n expect_equal(mul(mul(a, b), c), a * b * c)\n expect_equal(mul(a, mul(b, c)), a * b * c)\n})\n\n## Test passed 🥇\n\n\n\nSupported by\n\n\nStarCore Analytics",
"crumbs": [
"2025",
"2 Multiply"
]
},
{
"objectID": "cw0003countsheep.html",
"href": "cw0003countsheep.html",
"title": "\n3 If you can’t sleep, just count sheep!!\n",
"section": "",
"text": "8Kyu Tantangan #3/366 - 03 Feb 2025\nhttps://www.codewars.com/kata/5b077ebdaf15be5c7f000077\n\n3.1 Instruction\n\nGiven a non-negative integer, 3 for example, return a string with a murmur: \"1 sheep…2 sheep…3 sheep…\". Input will always be valid, i.e. no negative integers.\n\n\n3.2 YouTube Video\n\n\n3.3 Solution Code\nSolusi bar-bar\n\ncountSheep <- function(num){\n if(num == 0){\n return(\"\")\n } else {\n result <- \"\"\n for(i in 1:num){\n result <- paste(result, i, \" sheep...\", sep = \"\")\n }\n return(result)\n }\n}\n\nSolusi simple\n\ncountSheep <- function(num){\n ifelse(num == 0, \"\", paste(1:num, \"sheep...\", collapse = \"\"))\n}\n\n\n3.4 Test\n\nlibrary(testthat)\n\ntest_that(\"Fixed tests\", {\n expect_equal(countSheep(0), \"\")\n expect_equal(countSheep(1), \"1 sheep...\")\n expect_equal(countSheep(2), \"1 sheep...2 sheep...\")\n expect_equal(countSheep(3), \"1 sheep...2 sheep...3 sheep...\")\n})\n\n## Test passed 🥳\n\n\n\nSupported by\n\n\nStarCore Analytics",
"crumbs": [
"2025",
"3 Count Sheep"
]
},
{
"objectID": "cw0004reducegrow.html",
"href": "cw0004reducegrow.html",
"title": "\n4 Beginner - Reduce but Grow!\n",
"section": "",
"text": "8Kyu Tantangan #4/366 - 04 Feb 2025\nhttps://www.codewars.com/kata/57f780909f7e8e3183000078\n\n4.1 Instruction\n\nGiven a non-empty array of integers, return the result of multiplying the values together in order.\n\nExample:[1, 2, 3, 4] => 1 * 2 * 3 * 4 = 24\n\n4.2 YouTube Video\n\n\n4.3 Solution Code\nSolusi bar-bar\n\ngrow <- function(arr) {\n result <- 1\n for(i in arr){\n result <- result * i\n }\n return(result)\n}\n\nSolusi simple\n\n# grow <- function(arr) prod(arr)\ngrow <- prod\n\nFunction prod() adalah function untuk mengalikan nilai dari satu vector numeric, cara kerjanya sama persis dengan yang diinginkan. Oleh karena itu cukup meng-copy function prod() sebagai grow().\n\n4.4 Test\n\nlibrary(testthat)\ntest_that(\"basic tests\", {\n expect_equal(grow(c(1, 2, 3)), 6)\n expect_equal(grow(c(4, 1, 1, 1, 4)), 16)\n expect_equal(grow(c(2, 2, 2, 2, 2, 2)), 64)\n})\n\n## Test passed 🎉\n\n\n\nSupported by\n\n\nStarCore Analytics",
"crumbs": [
"2025",
"4 Beginner - Reduce but Grow!"
]
},
{
"objectID": "cw0005makenegative.html",
"href": "cw0005makenegative.html",
"title": "\n5 Return Negative\n",
"section": "",
"text": "8Kyu Tantangan #5/366 - 05 Feb 2025\nhttps://www.codewars.com/kata/55685cd7ad70877c23000102\n\n5.1 Instruction\n\nIn this simple assignment you are given a number and have to make it negative. But maybe the number is already negative?\n\nExamples\n\nmake_negative(1); # return -1 \nmake_negative(-5); # return -5\nmake_negative(0); # return 0\n\nNotes\n\nThe number can be negative already, in which case no change is required.\n\nZero (0) is not checked for any specific sign. Negative zeros make no mathematical sense.\n\n\n5.2 YouTube Video\n\n\n5.3 Solution Code\nSolusi bar-bar\n\nmake_negative <- function(num){\n ifelse(num <= 0, num, -1*num)\n}\n\nSolusi simple\n\nmake_negative <- function(num)-abs(num)\n\n\n5.4 Test\n\nlibrary(testthat)\n\ntest_that('Basic tests', {\n expect_equal(make_negative(42), -42)\n expect_equal(make_negative(-9), -9)\n expect_equal(make_negative(0), 0)\n expect_equal(make_negative(1), -1)\n expect_equal(make_negative(-1), -1)\n})\n\n## Test passed 🎉\n\n\n\nSupported by\n\n\nStarCore Analytics",
"crumbs": [
"2025",
"5 Return Negative"
]
}
]