코틀린으로 문제풀이

2023. 10. 17. 23:20알고리즘

응애 나 코틀린 처음해보는데

mobile programming 수업 때 코틀린 사용하니 과제로 주신 문제를 풀어보겠습니다.

1. Write a simple calculator program. The available arithmetic operators are +, -, *, and / operators. Operands and operator must be separated by whitespaces. The program must print “Cannot divide by 0” string when a user tries to “divide by 0” operation. Use “when” statement for operator branch.

2. Write a program that 1) takes a positive integer number N (less than 100), 2) creates an integer array with the size of N, 3) fills in this array using a set of random numbers ranging from 1~100, 4) prints out the numbers in this array. Note that this array MUST NOT have the duplicate numbers.

3. Write a program that initializes an array consisting of some string lines and counts the words in each string line.

4. Write a program that takes a string, rotates it one character at a time, and prints it all.

5. Write a code to check if a given string is palindrome or not.

6. Given a positive integer (10-99), write a program to check if the digits in the tens place and the ones place are the same.

7. Write a program to count the number of alphabets present in a given string using a Map collection.

Write a Data Class (e.g., Item class) to represent the following information:

 

-     Name property (String type)

-     Price property (Int type)

n  When this property is set, a message like “price set to [xxxx]. Are you serious?” must be printed out.

-     Share property (Int type)

-     Whenver a new instance is created, a message like “[name] item was created.” Must be printed out

 

Test code)

 

val item1 = Item(name="jinwoo1").apply{

    share = 100

    price = 500

}

. Create a List to include 10 randomly generated Item instances (the result of Question #2).

- Use for loop to create and add items into your list

- The name field should be “your name”+iteration number (1..10)

- The share filed should be 100

- the price field should be randomly set (0~1000)

 

Then, use forEach() method to print items in your list.

            

. Then, find the instances whose price is higher than 500 from your list.

- Use filter() method

- No for loop is allowed

. Finally, sort your list based on the price and print the sorted list using 1) run statement and 2) also statement.

- In run statement, you need to add code to sort the list based on the price, and get the string representation of the list

- In also statement, you need to printout the list with uppercase letters only

 

. Write a code to print the full pyramid using “*”.

- You are allowed to modify the body of run {} scope function in the following base code:

 

. Implement ShoppingCart class with the following methods :

- addProduct : Add a product to the shopping cart

- removeProduct : Remove all products matching the product name from the shopping cart

à If you do not use for/while loop to do this, +@ will be given as a bonus point.

- calculateTotalPrice: Calculate the total price of all products in the cart

 

- Shopping Cart can contain products represented by the Product class, which has product name (String) and price (Double) only.

 

Base code and test code are as follows:

 

Base code)

class Product()

class ShoppingCart {

}

 

Test code)

val cart = ShoppingCart()



val product1 = Product("Jean", 50.0)

val product2 = Product("Jean", 100.0)

val product3 = Product("Jean", 80.0)

val product4 = Product("Shoes", 70.0)

val product5 = Product("Pants", 90.0)

val product6 = Product("GPU", 2000.0)



cart.addProduct(product1)

cart.addProduct(product2)

cart.addProduct(product3)

cart.addProduct(product4)

cart.addProduct(product5)

cart.addProduct(product6)

cart.calculateTotalPrice()



cart.removeProduct("Jean")

cart.calculateTotalPrice()



cart.removeProduct("Shoes")

cart.calculateTotalPrice()

과제하느라 힘들어 죽을 뻔~

'알고리즘' 카테고리의 다른 글

자료구조,알고리즘,코딩테스트  (0) 2024.02.16