1 Soal 1

1. (CPL 11 dan CPL 13, 20%) Operasi dan Tipe Data Dasar

Buat program sederhana dalam R dan Python yang melakukan hal berikut:

  1. Menerima dua bilangan dari pengguna

  2. Menghitung dan menampilkan hasil:

  • Penjumlahan
  • Perkalian
  • Pembagian
  • Bilangan pertama pangkat bilangan kedua
  1. Menampilkan tipe data masing-masing hasil operasi
bil1 <- 2
bil2 <- 6

jumlah <- bil1 + bil2
kali <- bil1 * bil2
bagi <- bil1 / bil2
pangkat <- bil1 ^ bil2
cat("\n")
cat("Penjumlahan:", jumlah, "\n")
## Penjumlahan: 8
cat("Perkalian:", kali, "\n")
## Perkalian: 12
cat("Pembagian:", bagi, "\n")
## Pembagian: 0.3333333
cat("Pangkat:", pangkat, "\n\n")
## Pangkat: 64
cat("Hasil penjumlahan =", jumlah, "dengan tipe data", class(jumlah), "\n")
## Hasil penjumlahan = 8 dengan tipe data numeric
cat("Hasil perkalian =", kali, "dengan tipe data", class(kali), "\n")
## Hasil perkalian = 12 dengan tipe data numeric
cat("Hasil pembagian =", bagi, "dengan tipe data", class(bagi), "\n")
## Hasil pembagian = 0.3333333 dengan tipe data numeric
cat("Hasil pangkat =", pangkat, "dengan tipe data", class(pangkat), "\n")
## Hasil pangkat = 64 dengan tipe data numeric

2 Soal 2

  1. (CPL 1 dan CPL 2, 20%) Struktur Kendali (Control Flow)

Tulislah program dalam R dan Python yang:

  1. Menerima input nilai ujian dari pengguna (0–100)

  2. Menampilkan keterangan berdasarkan ketentuan berikut:

  • Nilai ≥ 85: “Sangat Baik”
  • Nilai 70–84: “Baik”
  • Nilai 60–69: “Cukup”
  • Nilai < 60: “Perlu Perbaikan”

nilai <- 74

if (nilai >= 85) {
  cat("Sangat Baik\n")
} else if (nilai >= 70 && nilai <= 84) {
  cat("Baik\n")
} else if (nilai >= 60 && nilai <= 69) {
  cat("Cukup\n")
} else {
  cat("Perlu Perbaikan\n")
}
## Baik

3 Soal 3

  1. (CPL_KU_01 dan CPL_KU_01, 20%) Fungsi dan Perulangan

Buatlah fungsi dalam R dan Python bernama kelipatan_genap(n) yang:

  1. Menerima input integer n

  2. Menggunakan loop untuk mencetak semua bilangan genap kelipatan 4 dari 1 hingga n

  3. Contoh output jika n = 20: 4, 8, 12, 16, 20

kelipatan_genap <- function(n) {
  hasil <- c()
  for (i in 1:n) {
    if (i %% 2 == 0 && i %% 4 == 0) {
      hasil <- c(hasil, i)
    }
  }
  cat(paste(hasil, collapse = ", "), "\n")
}

angka <- 40
kelipatan_genap(angka)
## 4, 8, 12, 16, 20, 24, 28, 32, 36, 40

4 Gabung dan Tampilkan Data

library(readr)
library(dplyr)

januari <- read_csv("data/penjualan-januari.csv")
februari <- read_csv("data/penjualan-februari.csv")
maret <- read_csv("data/penjualan-maret.csv")

df <- bind_rows(januari, februari, maret)

cat("Penggabungan Dataset:\n")
## Penggabungan Dataset:
print(df)
## # A tibble: 150 × 9
##    OrderID Tanggal    Produk        Kategori   Harga  Jumlah Total Pembeli Kota 
##      <dbl> <chr>      <chr>         <chr>      <chr>  <chr>  <lgl> <chr>   <chr>
##  1    1001 01-01-2024 Laptop A      <NA>       15000… empat  NA    Budi@   Band…
##  2    1002 02-01-2024 Tas Branded   Fashion    15000… 2      NA    Budi@   Sura…
##  3    1003 03-01-2024 Tas Branded   Aksesoris  15000… 3      NA    _anony… Band…
##  4    1004 04-01-2024 Kemeja        <NA>       15000… dua    NA    Andi123 Band…
##  5    1005 05-01-2024 -             Fashion    Rp750… 3      NA    _anony… Jaka…
##  6    1006 06-01-2024 Smartphone X  Fashion    15000… empat  NA    Sinta99 Band…
##  7    1007 07-01-2024 Sepatu 'Nike' Elektronik 1.200… 3      NA    _anony… Band…
##  8    1008 08-01-2024 -             Elektronik 250000 dua    NA    _anony… Jaka…
##  9    1009 09-01-2024 Smartphone X  Elektronik 15000… empat  NA    _anony… -    
## 10    1010 10-01-2024 Sepatu 'Nike' Aksesoris  Rp750… 2      NA    Sinta99 Sura…
## # ℹ 140 more rows
cat("\nJumlah baris:", nrow(df), "\n")
## 
## Jumlah baris: 150
cat("Jumlah kolom:", ncol(df), "\n")
## Jumlah kolom: 9

5 Data Cleaning

library(dplyr)
library(readr)
library(lubridate)
library(stringr)

# === 1. Baca dan Gabungkan Data ===
januari <- read_csv("data/penjualan-januari.csv", locale = locale(encoding = "ISO-8859-1"))
februari <- read_csv("data/penjualan-februari.csv", locale = locale(encoding = "ISO-8859-1"))
maret <- read_csv("data/penjualan-maret.csv", locale = locale(encoding = "ISO-8859-1"))

df <- bind_rows(januari, februari, maret)

# === 2. Format Tanggal ===
df$Tanggal <- as.Date(df$Tanggal, format = "%Y-%m-%d")

# === 3. Bersihkan dan Konversi Kolom Harga dan Jumlah ===
df$Harga <- as.numeric(str_replace_all(as.character(df$Harga), "[^0-9.]", ""))
## Warning: NAs introduced by coercion
df$Jumlah <- as.numeric(str_replace_all(as.character(df$Jumlah), "[^0-9.]", ""))

# === 4. Hitung Kolom Total ===
df$Total <- df$Harga * df$Jumlah

# === 5. Ganti Nilai Tidak Valid ===
nilai_tidak_valid <- c("-", "dua", "Rp", "_anonymous_")
df <- df %>%
  mutate(across(everything(), ~replace(.x, .x %in% nilai_tidak_valid, NA)))

# === 6. Hapus Baris Tanpa Nama Produk ===
df <- df %>%
  filter(!is.na(Produk), Produk != "", Produk != "-")

# === 7. Hapus Baris dengan NA di Kolom Wajib ===
kolom_wajib <- c("Tanggal", "Harga", "Jumlah", "Total")
df <- df %>%
  filter(if_all(all_of(kolom_wajib), ~!is.na(.)))

# === 8. Tampilkan Informasi dan Data Awal ===
cat(strrep("=", 40), "\n")
## ========================================
cat("INFORMASI DATA\n")
## INFORMASI DATA
cat(strrep("=", 40), "\n")
## ========================================
print(str(df))
## tibble [58 × 9] (S3: tbl_df/tbl/data.frame)
##  $ OrderID : num [1:58] 1002 1003 1010 1013 1016 ...
##  $ Tanggal : Date[1:58], format: "0002-01-20" "0003-01-20" ...
##  $ Produk  : chr [1:58] "Tas Branded" "Tas Branded" "Sepatu 'Nike'" "Laptop A" ...
##  $ Kategori: chr [1:58] "Fashion" "Aksesoris" "Aksesoris" "Aksesoris" ...
##  $ Harga   : num [1:58] 1.5e+07 1.5e+07 7.5e+02 7.5e+02 1.5e+07 1.5e+07 7.5e+02 7.5e+02 1.5e+07 1.5e+07 ...
##  $ Jumlah  : num [1:58] 2 3 2 2 2 1 2 3 2 3 ...
##  $ Total   : num [1:58] 30000000 45000000 1500 1500 30000000 15000000 1500 2250 30000000 45000000 ...
##  $ Pembeli : chr [1:58] "Budi@" NA "Sinta99" "Sinta99" ...
##  $ Kota    : chr [1:58] "Surabaya" "Bandung" "Surabaya" "Surabaya" ...
## NULL
cat("\n")
cat(strrep("=", 40), "\n")
## ========================================
cat("DATA YANG DIHASILKAN\n")
## DATA YANG DIHASILKAN
cat(strrep("=", 40), "\n")
## ========================================
print(head(df))
## # A tibble: 6 × 9
##   OrderID Tanggal    Produk        Kategori    Harga Jumlah  Total Pembeli Kota 
##     <dbl> <date>     <chr>         <chr>       <dbl>  <dbl>  <dbl> <chr>   <chr>
## 1    1002 0002-01-20 Tas Branded   Fashion     1.5e7      2 3   e7 Budi@   Sura…
## 2    1003 0003-01-20 Tas Branded   Aksesoris   1.5e7      3 4.50e7 <NA>    Band…
## 3    1010 0010-01-20 Sepatu 'Nike' Aksesoris   7.5e2      2 1.5 e3 Sinta99 Sura…
## 4    1013 0013-01-20 Laptop A      Aksesoris   7.5e2      2 1.5 e3 Sinta99 Sura…
## 5    1016 0016-01-20 Kemeja        Aksesoris   1.5e7      2 3   e7 <NA>    Sura…
## 6    1021 0021-01-20 Laptop A      Elektronik  1.5e7      1 1.5 e7 <NA>    Jaka…

6 Data Transformation

library(dplyr)
library(readr)
library(lubridate)
library(stringr)

# =======================
# Membaca dan Menggabungkan Data
# =======================

januari <- read_csv("data/penjualan-januari.csv", locale = locale(encoding = "ISO-8859-1"))
februari <- read_csv("data/penjualan-februari.csv", locale = locale(encoding = "ISO-8859-1"))
maret <- read_csv("data/penjualan-maret.csv", locale = locale(encoding = "ISO-8859-1"))

df <- bind_rows(januari, februari, maret)

# =======================
# Pembersihan dan Transformasi Data
# =======================

# Ubah kolom "Tanggal" ke Date, gagal parsing jadi NA
df <- df %>%
  mutate(
    Tanggal = as.Date(Tanggal),
    Bulan = format(Tanggal, "%Y-%m")
  )

# Bersihkan nilai non-numerik dan ubah ke numerik
df <- df %>%
  mutate(
    Harga = as.numeric(str_replace_all(as.character(Harga), "[^0-9.]", "")),
    Jumlah = as.numeric(str_replace_all(as.character(Jumlah), "[^0-9.]", "")),
    Total = Harga * Jumlah
  )
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `Harga = as.numeric(str_replace_all(as.character(Harga),
##   "[^0-9.]", ""))`.
## Caused by warning:
## ! NAs introduced by coercion
# =======================
# Analisis Data
# =======================

# Total penjualan per kategori
total_per_kategori <- df %>%
  group_by(Kategori) %>%
  summarise(Total = sum(Total, na.rm = TRUE), .groups = "drop")

# Jumlah transaksi per kota
transaksi_per_kota <- df %>%
  count(Kota, name = "Jumlah_Transaksi") %>%
  arrange(desc(Jumlah_Transaksi))

# Total penjualan per bulan
penjualan_per_bulan <- df %>%
  group_by(Bulan) %>%
  summarise(Total = sum(Total, na.rm = TRUE), .groups = "drop")

# =======================
# Output Hasil Analisis
# =======================

cat(strrep("=", 40), "\n")
## ========================================
cat("TOTAL PENJUALAN PER KATEGORI\n")
## TOTAL PENJUALAN PER KATEGORI
cat(strrep("=", 40), "\n")
## ========================================
print(total_per_kategori)
## # A tibble: 4 × 2
##   Kategori       Total
##   <chr>          <dbl>
## 1 Aksesoris  332264250
## 2 Elektronik 212756750
## 3 Fashion    225259750
## 4 <NA>       168762000
cat("\n", strrep("=", 40), "\n")
## 
##  ========================================
cat("JUMLAH TRANSAKSI PER KOTA\n")
## JUMLAH TRANSAKSI PER KOTA
cat(strrep("=", 40), "\n")
## ========================================
print(transaksi_per_kota)
## # A tibble: 4 × 2
##   Kota     Jumlah_Transaksi
##   <chr>               <int>
## 1 -                      38
## 2 Jakarta                38
## 3 Bandung                37
## 4 Surabaya               37
cat("\n", strrep("=", 40), "\n")
## 
##  ========================================
cat("TOTAL PENJUALAN PER BULAN\n")
## TOTAL PENJUALAN PER BULAN
cat(strrep("=", 40), "\n")
## ========================================
print(penjualan_per_bulan)
## # A tibble: 110 × 2
##    Bulan      Total
##    <chr>      <dbl>
##  1 0001-01        0
##  2 0001-02   250000
##  3 0001-03        0
##  4 0001-04        0
##  5 0002-01 30000000
##  6 0002-02     1500
##  7 0002-03 45001500
##  8 0002-04        0
##  9 0003-01 45000000
## 10 0003-02 15000000
## # ℹ 100 more rows

# =========================
# 📦 Load Library
# =========================
library(dplyr)
library(readr)
library(lubridate)
library(stringr)
library(ggplot2)
library(plotly)

# =========================
# 📁 Baca dan Gabungkan Data
# =========================
januari <- read_csv("data/penjualan-januari.csv", locale = locale(encoding = "ISO-8859-1"))
februari <- read_csv("data/penjualan-februari.csv", locale = locale(encoding = "ISO-8859-1"))
maret <- read_csv("data/penjualan-maret.csv", locale = locale(encoding = "ISO-8859-1"))

df <- bind_rows(januari, februari, maret)

# =========================
# 🧹 Pembersihan dan Transformasi Data
# =========================
df <- df %>%
  mutate(
    Tanggal = as.Date(Tanggal, format = "%Y-%m-%d"),
    Bulan = format(Tanggal, "%Y-%m"),
    Harga = as.numeric(str_replace_all(as.character(Harga), "[^0-9.]", "")),
    Jumlah = as.numeric(str_replace_all(as.character(Jumlah), "[^0-9.]", "")),
    Total = Harga * Jumlah
  )
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `Harga = as.numeric(str_replace_all(as.character(Harga),
##   "[^0-9.]", ""))`.
## Caused by warning:
## ! NAs introduced by coercion
# =========================
# 📊 Analisis Data
# =========================

# a. Total penjualan per kategori
total_per_kategori <- df %>%
  group_by(Kategori) %>%
  summarise(Total = sum(Total, na.rm = TRUE), .groups = "drop")

# b. Jumlah transaksi per kota
transaksi_per_kota <- df %>%
  count(Kota, name = "Jumlah_Transaksi") %>%
  arrange(desc(Jumlah_Transaksi))

# c. Total penjualan per bulan
penjualan_per_bulan <- df %>%
  group_by(Bulan) %>%
  summarise(Total = sum(Total, na.rm = TRUE), .groups = "drop")

# =========================
# 🎨 Visualisasi Data
# =========================

warna_biru_gradasi <- c('#1f77b4', '#2a80c9', '#3d8edb', '#539df0', '#6bb0ff', '#89c4ff', '#aed9ff')

# 1. Grafik Batang - Total Penjualan per Kategori
bar_plot <- ggplot(total_per_kategori, aes(x = Kategori, y = Total, fill = Kategori)) +
  geom_bar(stat = "identity") +
  scale_fill_manual(values = warna_biru_gradasi) +
  labs(title = "Total Penjualan per Kategori - Grafik Batang", x = "Kategori", y = "Total Penjualan") +
  theme_minimal()

ggplotly(bar_plot)
# 2. Grafik Donat - Jumlah Transaksi per Kota
donut_plot <- plot_ly(
  transaksi_per_kota,
  labels = ~Kota,
  values = ~Jumlah_Transaksi,
  type = 'pie',
  hole = 0.4,
  textinfo = 'label+percent',
  marker = list(colors = warna_biru_gradasi)
) %>%
  layout(title = 'Jumlah Transaksi per Kota - Grafik Donat')

donut_plot
# 3. Grafik Garis - Total Penjualan per Bulan
line_plot <- ggplot(penjualan_per_bulan, aes(x = Bulan, y = Total, group = 1)) +
  geom_line(color = "#1f77b4") +
  geom_point(color = "#1f77b4") +
  labs(title = "Total Penjualan per Bulan - Grafik Garis", x = "Bulan", y = "Total Penjualan") +
  theme_minimal()

ggplotly(line_plot)