Question

Coding Challenge: Write ONLY IN SCALA LANGUAGE PLEASE DONT ANSWER IF YOU'RE ANSWERING WITH SOME OTHER...

Coding Challenge: Write ONLY IN SCALA LANGUAGE PLEASE DONT ANSWER IF YOU'RE ANSWERING WITH SOME OTHER LANG

BACKGROUND Sometimes items cannot be shipped to certain zip codes, and the rules for these restrictions are stored as a series of ranges of 5 digit codes. For example if the ranges are: [94133,94133] [94200,94299] [94600,94699] Then the item can be shipped to zip code 94199, 94300, and 65532, but cannot be shipped to 94133, 94650, 94230, 94600, or 94299. Any item might be restricted based on multiple sets of these ranges obtained from multiple sources.

PROBLEM Given a collection of 5-digit ZIP code ranges (each range includes both their upper and lower bounds), provide an algorithm that produces the minimum number of ranges required to represent the same restrictions as the input.

NOTES The ranges above are just examples, your implementation should work for any set of arbitrary ranges Ranges may be provided in arbitrary order Ranges may or may not overlap Your solution will be evaluated on the correctness and the approach is taken, and adherence to coding standards and best practices EXAMPLES: If the input = [94133,94133] [94200,94299] [94600,94699] Then the output should be = [94133,94133] [94200,94299] [94600,94699] If the input = [94133,94133] [94200,94299] [94226,94399] Then the output should be = [94133,94133] [94200,94399] Please, help me out with the code challenge. It should be written in scala lang. Thanks.[please write the code in scala language no other lang]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Scala Programming:

Scala is a general-purpose programming language providing support for both object-oriented programming and functional programming.
The language has a strong static type system. Designed to be concise, many of Scala's design decisions are aimed to address criticisms of Java.
Problem Statement:

Sometimes items cannot be shipped to certain zip codes, and the rules for these restrictions are stored as a series of ranges of 5 digit codes.

For example if the ranges are:

[94133,94133] [94200,94299] [94600,94699]

Then the item can be shipped to zip code 94199, 94300, and 65532, but cannot be shipped to 94133, 94650, 94230, 94600, or 94299.

Any item might be restricted based on multiple sets of these ranges obtained from multiple sources.

ZipCodeRanges:

// Import the Range class from the range package
import range.Range;

// Import java.util package and its ArrayList class, ListIterator and List interface
import java.util.ArrayList;
import java.util.ListIterator;
// import java.util.List;

public class ZipCodeRanges {
// Static Variable
// Create a rangeList object initialized only once at the execution start from the ArrayList class to store a list of range objects
// ArrayList is like a dynamic array with a flexible size in length
// Add or remove as many range objects as I want
private static ArrayList<Range> rangeList = new ArrayList<Range>();

// Static Method
// Create a generateMinimumRanges method accessed by the ZipCodeRanges class directly to add or remove range objects to or from rangeList
public static void generateMinimumRanges(Range range) {
// If the number of range objects in rangeList is 0
if (rangeList.size() == 0) {
// Add the object to the List
rangeList.add(range);
} else {
// Create a rangeListIterator interface from the listIterator method of the ArrayList class
// The rangeListIterator interface allows me to traverse rangeList, modify the List during iteration, and obtain the Iterator's current position in the List
ListIterator<Range> rangeListIterator = rangeList.listIterator();

// Declare an addToRangeList variable to decide if the new range object will be added to rangeList
boolean addToRangeList = true;

// Iterate through range objects in rangeList
while (rangeListIterator.hasNext()) {
// Return the next range object (including the first range object) in the List
Range nextRange = rangeListIterator.next();

// If the range of a new range object is inside the range of the next range object
if (range.getLowerBound() >= nextRange.getLowerBound() && range.getUpperBound() <= nextRange.getUpperBound()) {
// Don't add the new range object to the List
addToRangeList = false;
// If the new range is outside the range of the next range object
} else if (range.getLowerBound() > nextRange.getUpperBound() || range.getUpperBound() < nextRange.getLowerBound()) {
// Add the new range object to the List
addToRangeList = true;
// If the new range is larger than the range of the next range object
} else if (range.getLowerBound() < nextRange.getLowerBound() && range.getUpperBound() > nextRange.getUpperBound()) {
// Remove that range object from the List
rangeListIterator.remove();

addToRangeList = true;
// If the new range overlaps lowerBound of the next range object
} else if (range.getLowerBound() < nextRange.getLowerBound() && range.getUpperBound() < nextRange.getUpperBound()) {
// Set upperBound of the new range object to upperBound of the next range object
range.setUpperBound(nextRange.getUpperBound());

rangeListIterator.remove();

addToRangeList = true;
// If the new range overlaps upperBound of the next range object
} else if (range.getLowerBound() > nextRange.getLowerBound() && range.getUpperBound() > nextRange.getUpperBound()) {
// Set lowerBound of the new range object to lowerBound of the next range object
range.setLowerBound(nextRange.getLowerBound());

rangeListIterator.remove();

addToRangeList = true;
}
}

if (addToRangeList) {
rangeList.add(range);
}
}
}

// Static Method
// Create a printMinimumRanges method accessed by the ZipCodeRanges class directly to print every range object in rangeList
public static void printMinimumRanges() {
if (rangeList.size() > 0) {
// Iterate through range objects in rangeList
for (Range range : rangeList) {
System.out.println(range.formatString());
}
}
}

// Static Method
// Create a getMinimumRangeList method accessed by the ZipCodeRanges class directly to return rangeList
/* public static List<Range> getMinimumRangeList() {
return rangeList;
} */

public static void main(String[] args) {
// Create range objects from the Range class
Range rangeOne = new Range(94133, 94133);
Range rangeTwo = new Range(94200, 94299);
Range rangeThree = new Range(94600, 94699);

// Call the generateMinimumRanges method on the ZipCodeRanges class
// Pass newly created range objects into the method
ZipCodeRanges.generateMinimumRanges(rangeOne);
ZipCodeRanges.generateMinimumRanges(rangeTwo);
ZipCodeRanges.generateMinimumRanges(rangeThree);

// Call the printMinimumRanges method on the ZipCodeRanges class
ZipCodeRanges.printMinimumRanges();

// Call the getMinimumRangeList method on the ZipCodeRanges class
// ZipCodeRanges.getMinimumRangeList();
}
}

Range:

// Create a range package to export the Range class
package range;

// Create a Range class for range object creation
public class Range {
private int upperBound;
private int lowerBound;

// Constructor
// Set lowerBound and upperBound value at the time of range object creation
public Range(int lowerBound, int upperBound) {
if (lowerBound <= upperBound) {
// Use "this" keyword to refer to the current range object
this.lowerBound = lowerBound;
this.upperBound = upperBound;
} else {
this.lowerBound = upperBound;
this.upperBound = lowerBound;
}
}

// Getter Method
// Create a getLowerBound method to get private lowerBound of the current range object from outside class
public int getLowerBound() {
return this.lowerBound;
}

// Getter Method
// Create a getUpperBound method to get private upperBound of the current range object from outside class
public int getUpperBound() {
return this.upperBound;
}

// Setter Method
// Create a setLowerBound method to set private lowerBound of the current range object from outside class
public void setLowerBound(int lowerBound) {
this.lowerBound = lowerBound;
}

// Setter Method
// Create a setUpperBound method to set private upperBound of the current range object from outside class
public void setUpperBound(int upperBound) {
this.upperBound = upperBound;
}

// Create a formatString method to format decimal integers into a string
public String formatString() {
return String.format("[%d,%d]", this.lowerBound, this.upperBound);
}
}

Output:

Input Data:

SELT6 TELTO EELT6 94133 00970 OO276 EITO EELTO 92276 DOCTO EELTO EEL76 00976 04133output:

Add a comment
Know the answer?
Add Answer to:
Coding Challenge: Write ONLY IN SCALA LANGUAGE PLEASE DONT ANSWER IF YOU'RE ANSWERING WITH SOME OTHER...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write the code in python, if you cannot answer all the question please dont anser, thanx very much. Please gimme a scr...

    Write the code in python, if you cannot answer all the question please dont anser, thanx very much. Please gimme a screen shot for code. Write clear code with comments and follow coding convention. Comments should include your name, student number and subject code on top of your code Question 1 Create a list to store all the subject codes that you are currently doing at UOW Then use for loop to display it in a table as in the...

  • Would appreciate the answer in the Java coding language please and thank you! 10d 10h left...

    Would appreciate the answer in the Java coding language please and thank you! 10d 10h left Java 7 1. Check the Structure Autocomplete Ready 1 > import java.io.*;... 10 ALL A binary tree uses a multi-node data structure where each node may have 0 to 2 child nodes, and has one stored value, its node number in this case. A tree may either be: 11 class Result { * Complete the 'isValid' function below. • An empty tree, the root...

  • Help write down below program with C++ language!!! Please... The Cipher Program Requirements An interactive program...

    Help write down below program with C++ language!!! Please... The Cipher Program Requirements An interactive program is required that allows a user to encode text using any of three possible ciphers. The three ciphers you are to offer are: Caesar, Playfair and Columnar Transposition. • The program needs to loop, repeating to ask the user if they wish to play with Caesar, Playfair or Columnar Transposition until the user wishes to stop the program. •For encoding, the program needs to...

  • Can you please help me with creating this Java Code using the following pseudocode? Make Change C...

    Can you please help me with creating this Java Code using the following pseudocode? Make Change Calculator (100 points + 5 ex.cr.)                                                                                                                                  2019 In this program (closely related to the change calculator done as the prior assignment) you will make “change for a dollar” using the most efficient set of coins possible. In Part A you will give the fewest quarters, dimes, nickels, and pennies possible (i.e., without regard to any ‘limits’ on coin counts), but in Part B you...

  • Chapter overview 1. Reasons for international trade Resources reasons Economic reasons Other reasons 2. Difference between...

    Chapter overview 1. Reasons for international trade Resources reasons Economic reasons Other reasons 2. Difference between international trade and domestic trade More complex context More difficult and risky Higher management skills required 3. Basic concept s relating to international trade Visible trade & invisible trade Favorable trade & unfavorable trade General trade system & special trade system Volume of international trade & quantum of international trade Commodity composition of international trade Geographical composition of international trade Degree / ratio of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT