Would like an algorithm in Python or just code in Python for : Goal: Given an or
ID: 3722351 • Letter: W
Question
Would like an algorithm in Python or just code in Python for :
Goal:
Given an ordered list of points which represent a polygon, return the number of points with integer (x, y) coordinates enclosed by the polygon.
Problem:
Provide a RESTful service which accepts as a POST of JSON an ordered list of points represented as (x, y) coordinates. The points define the perimeter of a polygon.
Return the number of points with integer (x, y) coordinates which are fully enclosed by the polygon. Points on the polygon itself are not included in the count – the points to be included in the count must be wholly within the area bounded by the polygon.
For example, assuming all points in the grid below are integers, the result would be “13”, as points on the polygon itself are not included.
The grid is at maximum 19x19. You can assume all points on the polygon are positive integers between 0 and 18 inclusive.
Example input:
{ “inList” : [{ “x” : 2,“y” : 1 } ,
{ “x” : 2,“y” : 4 } ,
{ “x” : 8,“y” : 4 },
{ “x” : 11, ”y” : 1 }
] }
Example output:
{ “count” : 13 }
Erroneous input (e.g. malformed JSON) should be handled gracefully.
Explanation / Answer
Answer:-
Code:-
open class Card executes Comparable<Card>{
private static last String IMAGE_FOLDER_DIR = "picture";
private static last String IMAGE_FORMAT = ".png";
private static last String BACK_IMAGE_DIR = ("picture/back_image.png");
private Image cardImage;
private Image backImage;
private SuitEnum suit;
private RankEnum rank;
open Card(){
}
open Card(SuitEnum suit, RankEnum rank){
this.suit = suit;
this.rank = rank;
String area = generateImageLocation();
attempt {
cardImage = new Image(location);
} get (Exception ex) {
System.out.println(String.format("cannot stack picture from: (%s)", area));
cardImage = invalid;
}
attempt {
backImage = new Image(BACK_IMAGE_DIR);
} get (Exception ex){
System.out.println(String.format("cannot stack picture from: (%s)", BACK_IMAGE_DIR));
backImage = invalid;
}
}
open SuitEnum getSuit() {
return suit;
}
open RankEnum getRank() {
return rank;
}
open Image getCardImage(){
return cardImage;
}
private String generateImageLocation(){
StringBuilder sb = new StringBuilder();
sb.append(IMAGE_FOLDER_DIR);
sb.append("/");
sb.append(suit.toString());
sb.append("_");
sb.append(rank.toString());
sb.append(IMAGE_FORMAT);
return sb.toString().toLowerCase();
}
@Override
open String toString(){
return (suit + " + rank);
}
open int compareTo(Card card) {
on the off chance that (this.rank.compareTo(card.rank) > 0){
return 1;
} else if (this.rank.compareTo(card.rank) < 0){
return - 1;
} else {
if(this.suit.compareTo(card.suit) > 0){
return 1;
} else if (this.suit.compareTo(card.suit) < 0){
return - 1;
} else {
return 0;
}
}
}
@Override
open int hashCode() {
last int prime = 31;
int result = 1;
result = prime * result + ((rank == invalid) ? 0 : rank.hashCode());
result = prime * result + ((suit == invalid) ? 0 : suit.hashCode());
return result;
}
@Override
open boolean equals(Object obj) {
on the off chance that (this == obj)
return genuine;
on the off chance that (obj == invalid)
return false;
in the event that (getClass() != obj.getClass())
return false;
Card other = (Card) obj;
in the event that (rank != other.rank)
return false;
on the off chance that (suit != other.suit)
return false;
return genuine;
}
open Image getBackImage() {
return backImage;
}
Deck Class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.