<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Foroogh Varmazyar</title>
    <description>The latest articles on DEV Community by Foroogh Varmazyar (@fvmzr).</description>
    <link>https://dev.to/fvmzr</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F524192%2F3aa582ed-3d79-40c4-93ec-5448bdb53637.jpg</url>
      <title>DEV Community: Foroogh Varmazyar</title>
      <link>https://dev.to/fvmzr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fvmzr"/>
    <language>en</language>
    <item>
      <title>Leetcode-Remove Element(Top Interview 150) Solution in Kotlin</title>
      <dc:creator>Foroogh Varmazyar</dc:creator>
      <pubDate>Fri, 20 Sep 2024 20:01:40 +0000</pubDate>
      <link>https://dev.to/fvmzr/leetcode-remove-elementtop-interview-150-solution-in-kotlin-13gh</link>
      <guid>https://dev.to/fvmzr/leetcode-remove-elementtop-interview-150-solution-in-kotlin-13gh</guid>
      <description>&lt;p&gt;Given an integer array &lt;em&gt;nums&lt;/em&gt; and an integer &lt;em&gt;val&lt;/em&gt;, remove all occurrences of &lt;em&gt;val&lt;/em&gt; in &lt;em&gt;nums&lt;/em&gt; i*&lt;em&gt;n-place&lt;/em&gt;*. The order of the elements may be changed. Then return the number of elements in &lt;em&gt;nums&lt;/em&gt; which are not equal to &lt;em&gt;val&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Consider the number of elements in &lt;em&gt;nums&lt;/em&gt; which are not equal to &lt;em&gt;val&lt;/em&gt; be &lt;em&gt;k&lt;/em&gt;, to get accepted, you need to do the following things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Change the array &lt;em&gt;nums&lt;/em&gt; such that the first &lt;em&gt;K&lt;/em&gt; elements of &lt;em&gt;nums&lt;/em&gt; contain the elements which are not equal to &lt;em&gt;val&lt;/em&gt;. The remaining elements of &lt;em&gt;nums&lt;/em&gt; are not important as well as the size of &lt;em&gt;nums&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Return &lt;em&gt;k&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Custom Judge:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The judge will test your solution with the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] nums = [...]; // Input array
int val = ...; // Value to remove
int[] expectedNums = [...]; // The expected answer with correct length.
                            // It is sorted with no values equaling val.

int k = removeElement(nums, val); // Calls your implementation

assert k == expectedNums.length;
sort(nums, 0, k); // Sort the first k elements of nums
for (int i = 0; i &amp;lt; actualLength; i++) {
    assert nums[i] == expectedNums[i];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If all assertions pass, then your solution will be accepted.&lt;/p&gt;

&lt;p&gt;Example 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,_,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 2.
It does not matter what you leave beyond the returned k (hence they are underscores).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example 2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,4,0,3,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4.
Note that the five elements can be returned in any order.
It does not matter what you leave beyond the returned k (hence they are underscores).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Constraints:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;0 &amp;lt;= nums.length &amp;lt;= 100&lt;br&gt;
    0 &amp;lt;= nums[i] &amp;lt;= 50&lt;br&gt;
    0 &amp;lt;= val &amp;lt;= 100&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The solution :
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    fun removeElement(nums: IntArray, `val`: Int): Int {
        var index = 0

        for (i in 0 until nums.size) {
            if (nums[i] != `val`) {
                nums[index++] = nums[i]
            }
        }
        return index
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Hackerrank-Kangaroo Solution in Kotlin</title>
      <dc:creator>Foroogh Varmazyar</dc:creator>
      <pubDate>Fri, 07 Apr 2023 16:57:44 +0000</pubDate>
      <link>https://dev.to/fvmzr/hackerrank-kangaroo-solution-in-kotlin-46nf</link>
      <guid>https://dev.to/fvmzr/hackerrank-kangaroo-solution-in-kotlin-46nf</guid>
      <description>&lt;p&gt;You are choreographing a circus show with various animals. For one act, you are given two kangaroos on a number line ready to jump in the positive direction (i.e, toward positive infinity). &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The first kangaroo starts at location x1  and moves at a rate of v1  meters per jump.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The second kangaroo starts at location x2 and moves at a rate of  v2  meters per jump.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You have to figure out a way to get both kangaroos at the same location at the same time as part of the show. If it is possible, return YES, otherwise return NO.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;x1=2&lt;br&gt;
v1=1&lt;br&gt;
x2=1&lt;br&gt;
v2=2&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;After one jump, they are both at x=3,(x1+v1=2+1,x2+v2=1+2)so the answer is YES.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function Description&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Complete the function kangaroo in the editor below.&lt;/p&gt;

&lt;p&gt;kangaroo has the following parameter(s): &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  int x1, int v1: starting position and jump distance for kangaroo 1&lt;/li&gt;
&lt;li&gt;  int x2, int v2: starting position and jump distance for kangaroo 2&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Returns&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  string: either YES or NO&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Input Format&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A single line of four space-separated integers denoting the respective values of x1,v1,x2  and v2&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constraints&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;0&amp;lt;x1&amp;lt;x2&amp;lt;10000&lt;br&gt;
1&amp;lt;v1&amp;lt;10000&lt;br&gt;
1&amp;lt;v2&amp;lt;10000&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sample Input 0&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 3 4 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Sample Output 0&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;YES
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation 0&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The two kangaroos jump through the following sequence of locations:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftke2z76so4xdchzfkfw7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftke2z76so4xdchzfkfw7.png" alt="Image description" width="670" height="211"&gt;&lt;/a&gt;&lt;br&gt;
From the image, it is clear that the kangaroos meet at the same location (number 12 on the number line) after same number of jumps (4 jumps), and we print YES.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sample Input 1&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 2 5 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Sample Output 1&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NO
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second kangaroo has a starting location that is ahead (further to the right) of the first kangaroo's starting location (i.e., x1&amp;gt;x2) Because the second kangaroo moves at a faster rate (meaning v2&amp;gt;v1)and is already ahead of the first kangaroo, the first kangaroo will never be able to catch up. Thus, we print NO. &lt;/p&gt;

&lt;h2&gt;
  
  
  Solution :
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun kangaroo(x1: Int, v1: Int, x2: Int, v2: Int): String {
    var start_k1 = x1+v1
    var startt_k2 = x2+v2
    var answer =0

     if(v1!= v2){
         var v = Math.abs(v2-v1)
         answer = (startt_k2-start_k1)%v
     } 

     if (v1&amp;lt;v2 || answer!=0 ||v1==v2){ 
         return "NO"
     }
       else{
          return "YES"
       }
   }

fun main(args: Array&amp;lt;String&amp;gt;) {
    val first_multiple_input = readLine()!!.trimEnd().split(" ")

    val x1 = first_multiple_input[0].toInt()

    val v1 = first_multiple_input[1].toInt()

    val x2 = first_multiple_input[2].toInt()

    val v2 = first_multiple_input[3].toInt()

    val result = kangaroo(x1, v1, x2, v2)

    println(result)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Hackerrank-Apple and Orange Solution in Kotlin</title>
      <dc:creator>Foroogh Varmazyar</dc:creator>
      <pubDate>Wed, 20 Jul 2022 21:11:01 +0000</pubDate>
      <link>https://dev.to/fvmzr/hackerrank-apple-and-orange-solution-in-kotlin-3e61</link>
      <guid>https://dev.to/fvmzr/hackerrank-apple-and-orange-solution-in-kotlin-3e61</guid>
      <description>&lt;p&gt;Sam's house has an apple tree and an orange tree that yield an abundance of fruit. Using the information given below, determine the number of apples and oranges that land on Sam's house.&lt;/p&gt;

&lt;p&gt;In the diagram below: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The red region denotes the house, where &lt;em&gt;s&lt;/em&gt;  is the start point, and &lt;em&gt;t&lt;/em&gt; is the endpoint. The apple tree is to the left of the house, and the orange tree is to its right. &lt;/li&gt;
&lt;li&gt;Assume the trees are located on &lt;em&gt;a&lt;/em&gt; single point, where the apple tree is at point &lt;em&gt;a&lt;/em&gt;, and the orange tree is at point &lt;em&gt;b&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;When a fruit falls from its tree, it lands &lt;em&gt;d&lt;/em&gt; units of distance from its tree of origin along the &lt;em&gt;x&lt;/em&gt; -axis. *A negative value of &lt;em&gt;d&lt;/em&gt; means the fruit fell &lt;em&gt;d&lt;/em&gt;  units to the tree's left, and &lt;em&gt;a&lt;/em&gt; positive value of &lt;em&gt;d&lt;/em&gt; means it falls &lt;em&gt;d&lt;/em&gt; units to the tree's right. *&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Given the value of &lt;em&gt;d&lt;/em&gt; for &lt;em&gt;m&lt;/em&gt; apples and &lt;em&gt;n&lt;/em&gt; oranges, determine how many apples and oranges will fall on Sam's house (i.e., in the inclusive range &lt;em&gt;[s,t]&lt;/em&gt;)?&lt;br&gt;
For example, Sam's house is between &lt;em&gt;s=7&lt;/em&gt; and &lt;em&gt;t=10&lt;/em&gt;. The apple tree is located at &lt;em&gt;a=4&lt;/em&gt;&lt;br&gt;
and the orange at &lt;em&gt;b=12&lt;/em&gt; There are &lt;em&gt;m=3 _apples and _n=3&lt;/em&gt; oranges. Apples are thrown &lt;em&gt;apples=[2,3,-4]&lt;/em&gt; units distance from a, and &lt;em&gt;oranges=[3,-2,-4]&lt;/em&gt; units distance. Adding each apple distance to the position of the tree, they land at [4+2,4+3,4+-4]=[6,7,0]. Oranges land at [12+3,12+-2,12+-4] =[15,10,8] One apple and two oranges land in the inclusive range 7-10 so we print&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Function Description&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Complete the countApplesAndOranges function in the editor below. It should print the number of apples and oranges that land on Sam's house, each on a separate line.&lt;/p&gt;

&lt;p&gt;countApplesAndOranges has the following parameter(s): &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;    s: integer, starting point of Sam's house location.&lt;/li&gt;
&lt;li&gt;    t: integer, ending location of Sam's house location.&lt;/li&gt;
&lt;li&gt;    a: integer, location of the Apple tree.&lt;/li&gt;
&lt;li&gt;    b: integer, location of the Orange tree.&lt;/li&gt;
&lt;li&gt;    apples: integer array, distances at which each apple falls from the tree.&lt;/li&gt;
&lt;li&gt;    oranges: integer array, distances at which each orange falls from the tree.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Input Format&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The first line contains two space-separated integers denoting the respective values of &lt;em&gt;s&lt;/em&gt; and &lt;em&gt;t&lt;/em&gt;.&lt;br&gt;
The second line contains two space-separated integers denoting the respective values of &lt;em&gt;a&lt;/em&gt; and &lt;em&gt;b&lt;/em&gt;.&lt;br&gt;
The third line contains two space-separated integers denoting the respective values of &lt;em&gt;m&lt;/em&gt; and &lt;em&gt;n&lt;/em&gt;.&lt;br&gt;
The fourth line contains space-separated integers denoting the respective distances that each apple falls from point &lt;em&gt;a&lt;/em&gt;.&lt;br&gt;
The fifth line contains &lt;em&gt;n&lt;/em&gt; space-separated integers denoting the respective distances that each orange falls from point b.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constraints&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;   1 &amp;lt; s, t, a, b, m, n &amp;lt; 10&lt;/li&gt;
&lt;li&gt;    -10 &amp;lt; d &amp;lt; 10&lt;/li&gt;
&lt;li&gt;    a &amp;lt; s &amp;lt; t &amp;lt; b&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Output Format&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Print two integers on two different lines:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;  The first integer: the number of apples that fall on Sam's house.&lt;/li&gt;
&lt;li&gt;    The second integer: the number of oranges that fall on Sam's house.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Sample Input 0&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;7 11
5 15
3 2
-2 2 1
5 -6

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Sample Output 0&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
1

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;ANSWER:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; fun countApplesAndOranges(
        s: Int,
        t: Int,
        a: Int,
        b: Int,
        apples: Array&amp;lt;Int&amp;gt;,
        oranges: Array&amp;lt;Int&amp;gt;
    ): Unit {
         var countApple = 0
         var countOrang = 0

         for (i in apples) {
             var appleDistance = a + i
             if (s &amp;lt;= appleDistance &amp;amp;&amp;amp; appleDistance &amp;lt;= t)
                 countApple = countApple + 1
         }
         for (i in oranges) {
             var orangeDistance = b + i
             if (s &amp;lt;= orangeDistance &amp;amp;&amp;amp; orangeDistance &amp;lt;= t)
                 countOrang = countOrang + 1
         }
         println(countApple)
         println(countOrang)
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Hackerrank-Time Conversion Solution in Kotlin</title>
      <dc:creator>Foroogh Varmazyar</dc:creator>
      <pubDate>Mon, 04 Jul 2022 09:09:01 +0000</pubDate>
      <link>https://dev.to/fvmzr/hackerrank-time-conversion-solution-in-kotlin-4ki3</link>
      <guid>https://dev.to/fvmzr/hackerrank-time-conversion-solution-in-kotlin-4ki3</guid>
      <description>&lt;h2&gt;
  
  
  problem
&lt;/h2&gt;

&lt;p&gt;Given a time in &lt;strong&gt;12&lt;/strong&gt;-hour AM/PM format, convert it to military (24-hour) time.&lt;br&gt;
Note: - 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock.&lt;br&gt;
-12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock. &lt;/p&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;s = '12:01:00PM'&lt;/strong&gt;&lt;br&gt;
Return '12:01:00'.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;s= '12:01:00AM'&lt;/strong&gt;&lt;br&gt;
Return '00:01:00'.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Function Description
&lt;/h2&gt;

&lt;p&gt;Complete the timeConversion function in the editor below. It should return a new string representing the input time in 24 hour format.&lt;/p&gt;

&lt;p&gt;timeConversion has the following parameter(s):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;string s: a time in &lt;strong&gt;12&lt;/strong&gt; hour format &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Returns&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;string: the time in &lt;strong&gt;24&lt;/strong&gt; hour format&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Input Format
&lt;/h2&gt;

&lt;p&gt;A single string &lt;strong&gt;s&lt;/strong&gt; containing a time in &lt;strong&gt;12&lt;/strong&gt; -hour clock format (i.e.: &lt;strong&gt;hh:mm:ssAM&lt;/strong&gt; or &lt;strong&gt;hh:mm:ssPM&lt;/strong&gt;)&lt;/p&gt;

&lt;h2&gt;
  
  
  Constraints
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt; All input times are valid&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Sample Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;07:05:45PM
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Sample Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;19:05:45
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Answer :
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
  fun timeConversion(s: String): String {

        val hour = s.substring(0, 2).toInt()
        val minsec = s.substring(2, 8)
        val timeZone = s.substring(8, 10)
        val time = s.substring(0, 8)

        var clock = return when (timeZone) {

            "AM" -&amp;gt; {
                if (hour == 12)
                    "00$minsec"
                else time
            }

            "PM" -&amp;gt; {
                if (hour == 12)
                    time
                else {
                    val sum = hour + 12
                    "$sum$minsec"
                }
            }

            else -&amp;gt; ""
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Hackerrank-Grading Students in kotlin</title>
      <dc:creator>Foroogh Varmazyar</dc:creator>
      <pubDate>Sun, 03 Jul 2022 19:59:54 +0000</pubDate>
      <link>https://dev.to/fvmzr/hackerrank-grading-students-in-kotlin-4kjh</link>
      <guid>https://dev.to/fvmzr/hackerrank-grading-students-in-kotlin-4kjh</guid>
      <description>&lt;p&gt;&lt;strong&gt;problem&lt;/strong&gt;&lt;br&gt;
HackerLand University has the following grading policy:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every student receives a grade in the inclusive range from 0 to 100 .&lt;/li&gt;
&lt;li&gt;Any grade less than 40 is a failing grade.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sam is a professor at the university and likes to round each student’s grade according to these rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the diﬀerence between the grade and the next multiple of 5 is less than 3, round grade up to the next multiple of 5.&lt;/li&gt;
&lt;li&gt;If the value of grade is less than 38, no rounding occurs as the result will still be a failing grade.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Examples&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;grade = 84 round to 85 (85–84 is less than 3)&lt;/li&gt;
&lt;li&gt;grade = 29 do not round (result is less than 40)&lt;/li&gt;
&lt;li&gt;grade = 57 do not round (60–57 is 3 or higher)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Given the initial value of grade for each of Sam’s n students, write code to automate the rounding process.&lt;/p&gt;

&lt;p&gt;Function Description&lt;/p&gt;

&lt;p&gt;Complete the function gradingStudents in the editor below.&lt;br&gt;
gradingStudents has the following parameter(s):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; int grades[n]: the grades before rounding&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Returns&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;int[n]: the grades after rounding as appropriate&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Input Format&lt;br&gt;
The ﬁrst line contains a single integer, n, the number of students. Each line of i the n subsequent lines contains a single integer, grade[i].&lt;/p&gt;

&lt;p&gt;Sample Input&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4 
73 
67 
38 
33
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sample Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;75 
67 
40 
33
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Answer :&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
  fun gradingStudents(grades: Array&amp;lt;Int&amp;gt;): Array&amp;lt;Int&amp;gt; {

        var listOfGrade: MutableList&amp;lt;Int&amp;gt; = ArrayList()

        for (grade in grades) {

            if (grade &amp;gt;= 38 &amp;amp;&amp;amp; (grade % 5) &amp;gt; 2) {
                var number = grade + (5 - (grade % 5))
                listOfNumber.add(number)
            } else {
                listOfNumber.add(grade)
            }
        }

        return listOfGrade.toTypedArray()
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Android Design Patterns: The Singleton Pattern</title>
      <dc:creator>Foroogh Varmazyar</dc:creator>
      <pubDate>Tue, 27 Jul 2021 13:59:40 +0000</pubDate>
      <link>https://dev.to/fvmzr/android-design-patterns-the-singleton-pattern-4pl6</link>
      <guid>https://dev.to/fvmzr/android-design-patterns-the-singleton-pattern-4pl6</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction Singleton Design Pattern&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Singleton design pattern  is categorized under creational design patterns. &lt;br&gt;
The Singleton Pattern  guarantees a class has one instance only and a global point of access to it is provided by that class. Anytime multiple classes or clients request for that class, they get the same instance of the class. &lt;/p&gt;

&lt;p&gt;The Singleton pattern solves two problems at the same time:&lt;/p&gt;

&lt;h6&gt;
  
  
  1) Ensure that a class has just a single instance.
&lt;/h6&gt;

&lt;p&gt;Why would anyone want to control how many instances a class has? The most common reason for this is to control access to some shared resource for example, a database or a file.&lt;br&gt;
 Here’s how it works: imagine that you created an object, but after a while decided to create a new one. Instead of receiving a fresh object, you’ll get the one you already created.&lt;br&gt;
Note that this behavior is impossible to implement with a regular constructor since a constructor call must always return a new object by design.&lt;/p&gt;

&lt;h6&gt;
  
  
  2) &lt;em&gt;Provide a global access point to that instance&lt;/em&gt;.
&lt;/h6&gt;

&lt;p&gt;Remember those global variables that you used to store some essential objects? While they’re very handy, they’re also very unsafe since any code can potentially overwrite the contents of those variables and crash the app.&lt;br&gt;
 Just like a global variable, the Singleton pattern lets you access some object from anywhere in the program. However, it also protects that instance from being overwritten by other code.&lt;br&gt;
 There’s another side to this problem: you don’t want the code that solves problem #1 to be scattered all over your program. It’s much better to have it within one class, especially if the rest of your code already depends on it.&lt;br&gt;
Nowadays, the Singleton pattern has become so popular that people may call something a singleton even if it solves just one of the listed problems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use case of Singleton design pattern&lt;/strong&gt;&lt;br&gt;
But before moving into the example, you need to know that there are two common steps in every Singleton implementation.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First step is to make the default constructor private, so it can prevent other objects from using the new operator with the Singleton class.&lt;/li&gt;
&lt;li&gt;Then implement a static creation method that acts as a constructor.This method calls the private constructor to create an object and saves it in a static field. So, whenever this method gets called, it will return the object.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: The most common use case for applying Singleton design pattern is when dealing with databases.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example 01: Classic Implementation
&lt;/h4&gt;

&lt;p&gt;It's quite easy to implement this pattern. The following code snippet shows how a Singleton is created.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; public class Singleton  {

    private static Singleton INSTANCE = null;

 // other instance variables can be here

    private Singleton() {};

    public static Singleton getInstance() {
       if (INSTANCE == null) {
           INSTANCE = new Singleton();
       }
       return(INSTANCE);
    }
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the code above, we have a static variable INSTANCE to hold an instance of the class. We also made the constructor private because we want to enforce noninstantiability—the class can only instantiate itself. The method getInstance() guarantees that the class is instantiated, if it has not been, and that it's returned to the caller. &lt;/p&gt;

&lt;h4&gt;
  
  
  Example 02:Creating a Single Instance of Retrofit
&lt;/h4&gt;

&lt;p&gt;In an Android app, you'll need a single global instance of a Retrofit object so that other parts of an app can use it to execute a network request without the need to create an instance every single time we need it. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; import retrofit2.Retrofit;
 import retrofit2.converter.gson.GsonConverterFactory;

 public class RetrofitClient {

 private static Retrofit retrofit = null;

 private RetrofitClient() {}

 public static Retrofit getClient(String baseUrl) {
   if (retrofit==null) {
      retrofit = new Retrofit.Builder()
      .baseUrl(baseUrl) 
      .addConverterFactory(GsonConverterFactory.create())
      .build();
   }
   return retrofit;
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So anytime client A calls RetrofitClient.getClient(), it creates the instance if it has not been created already, and then when client B calls this method, it checks if the Retrofit instance already exists. If so, it returns the instance to client B instead of creating a new one. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note :&lt;/strong&gt; The main problem with above method is that it is not thread safe. &lt;br&gt;
To solve the problem, it is better to implement the following method&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitClient {
private static volatile Retrofit retrofit = null;
private RetrofitClient() {}   

public static Retrofit getClient(String baseUrl) {
  if (retrofit==null) {
    synchronized (RetrofitClient.class) {
      retrofit = new Retrofit.Builder()
       .baseUrl(baseUrl)
      .addConverterFactory(GsonConverterFactory.create())
       .build();
    }
  }        
  return retrofit;
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We have declared the retrofit volatile which ensures that multiple threads offer the retrofit variable correctly when it is being initialized to RetrofitClient instance.&lt;/p&gt;

&lt;h6&gt;
  
  
  Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
&lt;/h6&gt;

</description>
    </item>
    <item>
      <title>Common Design Patterns in Android</title>
      <dc:creator>Foroogh Varmazyar</dc:creator>
      <pubDate>Sat, 24 Jul 2021 10:14:08 +0000</pubDate>
      <link>https://dev.to/fvmzr/common-design-pattern-in-android-part-1-40nd</link>
      <guid>https://dev.to/fvmzr/common-design-pattern-in-android-part-1-40nd</guid>
      <description>&lt;p&gt;A design pattern provides a general reusable solution for the common problems that occur in software design. The pattern typically shows relationships and interactions between classes or objects. The idea is to speed up the development process by providing well tested, proven development/design paradigms. Design patterns are programming language independent strategies for solving a common problem. That means a design pattern represents an idea, not a particular implementation. By using design patterns, you can make your code more flexible, reusable, and maintainable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Types of Design Patterns
&lt;/h3&gt;

&lt;p&gt;There are mainly three types of design patterns: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Creational patterns&lt;/strong&gt;: How you create objects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Structural patterns&lt;/strong&gt;: How you compose objects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Behavioral patterns&lt;/strong&gt;: How you coordinate object interactions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It’s not mandatory to always implement design patterns in your project. Design patterns are not meant for project development. Design patterns are meant for common problem-solving. Whenever there is a need, you have to implement a suitable pattern to avoid such problems in the future. To find out which pattern to use, you just have to try to understand the design patterns and their purposes. Only by doing that, you will be able to pick the right one.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creational Patterns :
&lt;/h3&gt;

&lt;p&gt;These design patterns are all about class instantiation or object creation. These patterns can be further categorized into Class-creational patterns and object-creational patterns.&lt;/p&gt;

&lt;p&gt;Common Creational design patterns in Android are:&lt;br&gt;
    * &lt;strong&gt;Builder :&lt;/strong&gt; Separates object construction from its representation &lt;br&gt;
    * &lt;strong&gt;Singleton :&lt;/strong&gt; A class of which only a single instance can exist &lt;br&gt;
    * &lt;strong&gt;Factory Method :&lt;/strong&gt; Creates an instance of several derived classes&lt;/p&gt;

&lt;h3&gt;
  
  
  Structural Patterns :
&lt;/h3&gt;

&lt;p&gt;These design patterns are about organizing different classes and objects to form larger structures and provide new functionality&lt;/p&gt;

&lt;p&gt;Common Structural design patterns in Android are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Adapter :&lt;/strong&gt; Match interfaces of different classes &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Facade :&lt;/strong&gt; A single class that represents an entire subsystem &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Decorator :&lt;/strong&gt; Add responsibilities to objects dynamically&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Composite :&lt;/strong&gt; A tree structure of simple and composite objects &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Behavioral Patterns :
&lt;/h3&gt;

&lt;p&gt;Behavioral patterns are about identifying common communication patterns between objects and realizing these patterns. &lt;/p&gt;

&lt;p&gt;Common Behavioral patterns in Android are :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Command :&lt;/strong&gt; Encapsulate a command request as an object&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Observer :&lt;/strong&gt; A way of notifying change to a number of classes &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Strategy :&lt;/strong&gt; Encapsulates an algorithm inside a class &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;State :&lt;/strong&gt; Alter an object's behavior when its state changes&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
  </channel>
</rss>
