Question

In: Computer Science

Q, Haskell. I need to define function encode and decode. Details about the functions are provided...

Q, Haskell.

I need to define function encode and decode. Details about the functions are provided in code.

-- | encode
--
-- Given a string, return a list of encoded values of type (Int,Char)
--
-- >>> encode ['a','a','a','a','b','c','c','a','a','d','e','e','e','e']
-- [(4,'a'),(1,'b'),(2,'c'),(2,'a'),(1,'d'),(4,'e')]
--
-- >>> encode "hello"
-- [(1,'h'),(1,'e'),(2,'l'),(1,'o')]
--
-- >>> encode []
-- []
--
encode :: String -> [(Int,Char)]
encode = undefined

-- | decode
--
-- Given a list of encoded values of type (Int,Char), generate a string corresponding to
-- this encoding.
--
-- If the first element of any pair in the list is equal to zero or negative, 
-- skip the corresponding character in the output string, 
-- while still providing decodings for the remaining characters.
-- 
--
-- >>> decode [(4,'a'),(1,'b'),(2,'c'),(2,'a'),(1,'d'),(4,'e')]
-- "aaaabccaadeeee"
--
-- >>> decode []
-- ""
--
-- >>> decode [(-4,'a')]
-- ""
--
-- >>> decode [(3,'c'),(-4,'a'),(5,'b')]
-- "cccbbbbb"
--
-- >>>decode [(3,'c'),(0,'a'),(5,'b')]
-- "cccbbbbb"
--
-- prop> \x -> x == decode (encode x)
-- 
decode :: [(Int,Char)] -> String
decode = undefined

Solutions

Expert Solution

import java.util.*;

public class EncodeDecode {
  
   static class Result
   {
       int freq;
       char data;
      
       public Result(int freq, char data)
       {
           this.freq = freq;
           this.data = data;
       }
      
       public String toString()
       {
           return "(" + freq + ", " + "'" + data + "'" + ")";
       }
   }
  
   public static List<Result> encode(String str)
   {
       if(str == null)
           return null;
      
       List<Result> list = new ArrayList<>();
      
       if(str.length() < 1)
           return list;
      
       int freq = 1;
       char ch = str.charAt(0);
       EncodeDecode.Result node;      
       for(int i = 1; i <= str.length();i++)
       {
           while(i < str.length() && str.charAt(i) == ch)
           {
               freq++;
               i++;
           }
           node = new EncodeDecode.Result(freq, ch);
           list.add(node);
          
           if(i < str.length())
           {
               ch = str.charAt(i);
               freq = 1;
           }              
       }
       return list;
   }
  
   public static String decode(List<Result> list)
   {
       if(list == null)
           return null;
      
       StringBuilder str = new StringBuilder();
       if(list.size() < 1)
           return str.toString();
      
       for(Result res : list)
       {
           if(res.freq < 1)
               continue;
          
           while(res.freq-- > 0)
           {
               str.append(res.data);
           }
       }
      
       return str.toString();
   }

   public static void main(String[] args) {
       // Driver Method
       List<Result> ans = encode("aabccaadeeee");
       System.out.println(ans);
      
       System.out.println(decode(ans));
      
   }

}


Related Solutions

Define and discuss both ASTM grain size and Vickers hardness in details. I need a full...
Define and discuss both ASTM grain size and Vickers hardness in details. I need a full and detailed answer with references.
Q) compare between types of transducer(ultrasound) and their applications? I need more details, other than the...
Q) compare between types of transducer(ultrasound) and their applications? I need more details, other than the linear transducer, phased array transducer... help me):
Hi,I need the fastest possible answer, I need solution for this issue with all the details...
Hi,I need the fastest possible answer, I need solution for this issue with all the details just nu .BR/Ha Q. Answer whether the following examples of Foreign Direct Investment (FDI) describe horizontal or vertical FDI. Explain your answers. a) Volvo builds a car factory in Austin, Texas, which is similar to Volvo’s factory in Gothenburg. The main purpose is to avoid US import tariffs. b) H&M relocates production of its clothes from Sweden to Bangladesh to reduce wage costs. (5...
Hi,I need the fastest possible answer, I need solution for this issue with all the details...
Hi,I need the fastest possible answer, I need solution for this issue with all the details just nu .BR/Ha 1-Give an overview of the studies testing discrimination in education.
I need to write a function that takes a user-provided string like 1-3-5, and output a...
I need to write a function that takes a user-provided string like 1-3-5, and output a corresponding series of letters, where A is assigned to 1, B is assigned to 2, C is assigned to 3, etc. So in the case of 1-3-5 the output would be ACE. For 2-3-4, it should print BCD. For ?-3-4 or --3-4 it should still print BCD. **CANNOT USE LISTS, SETS, DICTS, ETC. CANNOT USE SPLIT FUNCTION. ** Here is the code I have...
Q: I NEED A CRITICAL ANALYSIS ABOUT THIS ARTICAL The article: How to handle culture shock...
Q: I NEED A CRITICAL ANALYSIS ABOUT THIS ARTICAL The article: How to handle culture shock when moving abroad Disguised as fatigue, homesickness, frustration, disorientation - or just feeling overwhelmed by a new way of doing things. Whether you’re going abroad for a week, or moving to a new country: culture shock can affect us all, no matter how well travelled you are. I’ve lived in 3 different countries, and every time I move it gets slightly easier. Our initial...
Q. Discuss the details of secretion, regulation and function on target tissue of hormone Oxytocin.
Q. Discuss the details of secretion, regulation and function on target tissue of hormone Oxytocin.
Python 3 Forming Functions Define and complete the functions described below. * function name: say_hi *...
Python 3 Forming Functions Define and complete the functions described below. * function name: say_hi * parameters: none * returns: N/A * operation: just say "hi" when called. * expected output: >>> say_hi() hi * function name: personal_hi * parameters: name (string) * returns: N/A * operation: Similar to say_hi, but you should include the name argument in the greeting. * expected output: >>> personal_hi("Samantha") Hi, Samantha * function name: introduce * parameters: name1 (string) name2 (string) * returns: N/A...
Python 3 Functions that give answers Define and complete the functions described below. * function name:...
Python 3 Functions that give answers Define and complete the functions described below. * function name: get_name * parameters: none * returns: string * operation: Here, I just want you to return YOUR name. * expected output: JUST RETURNS THE NAME...TO VIEW IT YOU CAN PRINT IT AS BELOW >>> print(get_name()) John * function name: get_full_name * parameters: fname (string) lname (string) first_last (boolean) * returns: string * operation: Return (again, NOT print) the full name based on the first...
4. Consider the following production functions: (i) Q = 4K2L2 (ii) Q = 2K + 4L...
4. Consider the following production functions: (i) Q = 4K2L2 (ii) Q = 2K + 4L (iii) Q = min(4K, 5L) a. Graph an isoquant for Q = 400 for each of the production functions above. b. In few words, explain what does the MRTS tell us about a production function? c. Find the MRTS for production functions (i) and (ii). d. Find the MRTS for production function (iii)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT