0
>     1   package com.linecorp.exam
>     2  
>     3   import android.os.Bundle
>     4   import android.widget.TextView
>     5   import android.app.Activity 
>     6   import android.graphics.Color
>     7   import android.view.Gravity
>     8   import android.view.View
>     9   import android.view.ViewGroup
>     10  import android.widget.BaseAdapter
>     11  import android.widget.ListView
>     12   
>     13  class MainActivity : Activity() {
>     14   
>     15      enum class taskstate {
>     16          todo,
>     17          done
>     18      }
>     19   
>     20      var tasklist = mutableListOf<Pair<String, taskstate>>()
>     21   
>     22      private lateinit var myadapter: Myadapter
>     23   
>     24      override fun onCreate(savedInstanceState: Bundle?) {
>     25          super.onCreate(savedInstanceState)
>     26          setContentView(R.layout.activity_main)
>     27   
>     28          myadapter = Myadapter()
>     29          val listView = findViewById<ListView>(R.id.list_view)
>     30          listView.adapter = myadapter
>     31   
>     32          tasklist.clear()
>     33          var i = 0
>     34          todoRepository.instance.fetch_all().forEach { t ->
>     35              tasklist.add(i++, t)
>     36              myadapter.notifyDataSetChanged()
>     37          }
>     38      }
>     39   
>     40      override fun onDestroy() {
>     41          tasklist.clear()
>     42      }
>     43   
>     44      inner class Myadapter : BaseAdapter() {
>     45   
>     46          private lateinit var convertView: View
>     47   
>     48          override fun getCount(): Int {
>     49              return tasklist.size
>     50          }
>     51   
>     52          override fun getItem(position: Int): Any {
>     53              val li = tasklist.filter { it.second == taskstate.todo } +
>     54                          tasklist.filter { it.second == taskstate.done }
>     55              return li[position]
>     56          }
>     57   
>     58          override fun getItemId(position: Int): Long {
>     59              return 0
>     60          }
>     61   
>     62          override fun getView(position: Int, convertView: View?, container: ViewGroup?): View {
>     63              this.convertView = if (convertView == null) {
>     64                  layoutInflater.inflate(R.layout.list_item, container, false)
>     65              } else {
>     66                  convertView
>     67              }
>     68   
>     69              val i = getItem(position) as Pair<String, taskstate>
>     70              this.convertView.findViewById<TextView>(R.id.item_label)
>     71                  .apply {
>     72                      when (i.second) {
>     73                          taskstate.todo -> {
>     74                              setText("TODO")
>     75                              setBackgroundColor(Color.YELLOW)
>     76                          }
>     77                          else -> {
>     78                              setText("DONE")
>     79                          }
>     80                      }
>     81                  }
>     82              this.convertView.findViewById<TextView>(R.id.item_text)
>     83                  .setText(i.first)
>     84   
>     85              return convertView!!
>     86          }
>     87      }
>     88  }`enter code here`

This was a question I got when I appeared for an test. Unfortunately I failed :) The question was how to improve this Kotlin Android code and add necessary comments if needed. (please dont consider this line ipsum dfgsdndd gfnjfn vjfnvkf fjnvkfv vnkdkvd dndk. sds dshdsd shdahd sdiauhd basudsua saudhaus sahdsuahd ashdoahsd shdoahd ashdosahd asdhoaishd )

1 Answer 1

1

The problem here is you are returning total if s1<s2 while that's not the case. Even if you find one such case, that's not the end of string, you should continue processing. Instead of returning, you should increment i to i+2 as you have already processed i+1. Since you can't do such increment in between a for loop, so you will have to go with a while loop.

fun romanToInt(s: String): Int {
    val map = mapOf('I' to 1, 'V' to 5, 'X' to 10, 'L' to 50, 'C' to 100, 'D' to 500, 'M' to 1000)
    var total = 0
    var i = 0

    while (i < s.length) {
        val current = map[s[i]]!!
        val next = if (i != s.lastIndex) map[s[i + 1]]!! else 0
        if (current >= next) {
            total += current
            i++
        } else {
            total += next - current
            i += 2
        }
    }
    return total
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.