New to rust, and i tumble on something i don't get.
Given an array of &str, the method binary_search miss elements that are within the array.
I must do something wrong but can't figure out what.
const CARDS_AS_STR: [&'static str; 52] = [
"2c", "2d", "2h", "2s", "3c", "3d", "3h", "3s", "4c", "4d", "4h", "4s",
"5c", "5d", "5h", "5s", "6c", "6d", "6h", "6s", "7c", "7d", "7h", "7s",
"8c", "8d", "8h", "8s", "9c", "9d", "9h", "9s", "Tc", "Td", "Th", "Ts",
"Jc", "Jd", "Jh", "Js", "Qc", "Qd", "Qh", "Qs", "Kc", "Kd", "Kh", "Ks",
"Ac", "Ad", "Ah", "As",
];
fn main() {
println!("{:?}", CARDS_AS_STR.binary_search(&"9c")); // Ok(28)
println!("{:?}", CARDS_AS_STR.binary_search(&"Kc")); // Err(40) ?
println!("{:?}", CARDS_AS_STR.binary_search(&CARDS_AS_STR[36])); // Err(32) ?¿?
}
I've tried binary_search_by to end up with the same result.
playground
Binary search only works on sorted arrays, your's isn't. Using:
const CARDS_AS_STR: [&'static str; 52] = [
"2c", "2d", "2h", "2s", "3c", "3d", "3h", "3s", "4c", "4d", "4h", "4s", "5c", "5d", "5h", "5s",
"6c", "6d", "6h", "6s", "7c", "7d", "7h", "7s", "8c", "8d", "8h", "8s", "9c", "9d", "9h", "9s",
"Ac", "Ad", "Ah", "As", "Jc", "Jd", "Jh", "Js", "Kc", "Kd", "Kh", "Ks", "Qc", "Qd", "Qh", "Qs",
"Tc", "Td", "Th", "Ts",
];
Will work
Related
I'm trying to figure out how to return the value for participantId when inputting the value for summonerName. I thought of something like participantIdentities.index(...) but I'm working with a list of dictionaries that contain nested dictionaries, so I'm really not sure on this.
Example:
Input: dd god
Output: 1
"participantIdentities": [
{
"participantId": 1,
"player": {
"accountId": "g7cSjy8G4hMab3ayDaY8cqOSSjztYvktybRT_XgkBsJUSD0",
"currentAccountId": "g7cSjy8G4hMab3ayDaY8cqOSSjztYvktybRT_XgkBsJUSD0",
"currentPlatformId": "NA1",
"matchHistoryUri": "/v1/stats/player_history/NA1/233825986",
"platformId": "NA1",
"profileIcon": 1453,
"summonerId": "ICw1u2Kv-lHR_bjaNPa6BHbpwGT5rqJJJIVfiqcpbBdy9LM",
"summonerName": "dd god"
}
},
{
"participantId": 2,
"player": {
"accountId": "ZJ3NohMpa_FZHHSxyFBOxjyuU6JpL-LEbctTPV2pDeuNbw",
"currentAccountId": "oS_oSZLMTC3ZYVYiehAR6ZA4Gly-qq_WT_c5uXvQRRryzlw",
"currentPlatformId": "NA1",
"matchHistoryUri": "/v1/stats/player_history/EUW1/22181515",
"platformId": "EUW1",
"profileIcon": 3271,
"summonerId": "PlJSBls3iy0emnKRlwFZqvye8Plwnbp5ngG_NJ6JQmDI1nE",
"summonerName": "TSM Bjergsen"
}
}
]
You can do something like this:
dict_ = {"participantIdentities": [
{
"participantId": 1,
"player": {
"accountId": "g7cSjy8G4hMab3ayDaY8cqOSSjztYvktybRT_XgkBsJUSD0",
"currentAccountId": "g7cSjy8G4hMab3ayDaY8cqOSSjztYvktybRT_XgkBsJUSD0",
"currentPlatformId": "NA1",
"matchHistoryUri": "/v1/stats/player_history/NA1/233825986",
"platformId": "NA1",
"profileIcon": 1453,
"summonerId": "ICw1u2Kv-lHR_bjaNPa6BHbpwGT5rqJJJIVfiqcpbBdy9LM",
"summonerName": "dd god"
}
},
{
"participantId": 2,
"player": {
"accountId": "ZJ3NohMpa_FZHHSxyFBOxjyuU6JpL-LEbctTPV2pDeuNbw",
"currentAccountId": "oS_oSZLMTC3ZYVYiehAR6ZA4Gly-qq_WT_c5uXvQRRryzlw",
"currentPlatformId": "NA1",
"matchHistoryUri": "/v1/stats/player_history/EUW1/22181515",
"platformId": "EUW1",
"profileIcon": 3271,
"summonerId": "PlJSBls3iy0emnKRlwFZqvye8Plwnbp5ngG_NJ6JQmDI1nE",
"summonerName": "TSM Bjergsen"
}
}
]
}
input_val = "dd god"
identities = dict_['participantIdentities']
for p in identities:
if p['player']['summonerName'] == input_val:
print(p['participantId']
With the for loop you can iterate through your list of dictionaries and your nested dictionaries are accessable like a more dimensional array:
dict['a']['b'][...]
I'm making a card game in python, and having trouble dealing the cards out to players (for simplification, cards are just strings, where AH is ace of hearts). I'm trying to deal one card to each player in turn using modulo. However, for each iteration of the for loop every player is getting the same card, not just one player. I don't understand why - if anyone could help I'd be v appreciative!
class Player:
def __init__(self, hand = []):
self.hand = hand
deck = ["AS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS",
"AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH",
"AC", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC",
"AD", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD"]
player1 = Player()
player2 = Player()
player3 = Player()
players = [player1, player2, player3]
def dealCards(deck, players):
for i in range(len(deck)):
j = i % len(players)
players[j].hand.append(deck[i])
calculateHandSize(deck, players)
This gets the result I believe you want. There were a few issues I fixed:
class Player:
def __init__(self, hand=None):
if hand is None:
self.hand = []
else:
self.hand = hand
deck = ["AS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS",
"AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH",
"AC", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC",
"AD", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD"]
player1 = Player()
player2 = Player()
player3 = Player()
players = [player1, player2, player3]
def deal_cards(deck, players):
for i, card in enumerate(deck):
j = i % len(players)
players[j].hand.append(deck[i])
deal_cards(deck, players)
for p in players:
print(p.hand)
Where the hands look like:
['AS', '4S', '7S', '10S', 'KS', '3H', '6H', '9H', 'QH', '2C', '5C', '8C', 'JC', 'AD', '4D', '7D', '10D', 'KD']
['2S', '5S', '8S', 'JS', 'AH', '4H', '7H', '10H', 'KH', '3C', '6C', '9C', 'QC', '2D', '5D', '8D', 'JD']
['3S', '6S', '9S', 'QS', '2H', '5H', '8H', 'JH', 'AC', '4C', '7C', '10C', 'KC', '3D', '6D', '9D', 'QD']
I think the problem was in the use of range where enumerate is a better choice. I did not dig in to prove exactly what was wrong. I also changed the default arg in the __init__ to be more Python happy.
The solution could be compacted more and probably streamlined.
I am working with a JSON object, and want to convert object.hours to relational table, based on Spark SQL dataframe/dataset.
I tried to use "explode", which is not really supporting the "structs array".
The json object is below:
{
"business_id": "abc",
"full_address": "random_address",
"hours": {
"Monday": {
"close": "02:00",
"open": "11:00"
},
"Tuesday": {
"close": "02:00",
"open": "11:00"
},
"Friday": {
"close": "02:00",
"open": "11:00"
},
"Wednesday": {
"close": "02:00",
"open": "11:00"
},
"Thursday": {
"close": "02:00",
"open": "11:00"
},
"Sunday": {
"close": "00:00",
"open": "11:00"
},
"Saturday": {
"close": "02:00",
"open": "11:00"
}
}
}
To a relational table like below,
CREATE TABLE "business_hours" (
"id" integer NOT NULL PRIMARY KEY,
"business_id" integer NOT NULL FOREIGN KEY REFERENCES "businesses",
"day" integer NOT NULL,
"open_time" time,
"close_time" time
)
You can do this using this trick:
import org.apache.spark.sql.types.StructType
val days = df.schema
.fields
.filter(_.name=="hours")
.head
.dataType
.asInstanceOf[StructType]
.fieldNames
val solution = df
.select(
$"business_id",
$"full_address",
explode(
array(
days.map(d => struct(
lit(d).as("day"),
col(s"hours.$d.open").as("open_time"),
col(s"hours.$d.close").as("close_time")
)):_*
)
)
)
.select($"business_id",$"full_address",$"col.*")
scala> solution.show
+-----------+--------------+---------+---------+----------+
|business_id| full_address| day|open_time|close_time|
+-----------+--------------+---------+---------+----------+
| abc|random_address| Friday| 11:00| 02:00|
| abc|random_address| Monday| 11:00| 02:00|
| abc|random_address| Saturday| 11:00| 02:00|
| abc|random_address| Sunday| 11:00| 00:00|
| abc|random_address| Thursday| 11:00| 02:00|
| abc|random_address| Tuesday| 11:00| 02:00|
| abc|random_address|Wednesday| 11:00| 02:00|
+-----------+--------------+---------+---------+----------+
Question for the NetSuite experts:
Before my time, my current company decided to move to NetSuite. I'm being asked to retrieve customer data from our in-house SaaS and reformat it for import to NetSuite. Our NetSuite consultant says the customer import feature requires us to specify country by country name -- and, of course, NetSuite's built-in list of country names differs from our internal list which some developer pulled long ago from ISO 3166.
We asked the consultant why NetSuite doesn't import based upon two-letter ISO 3166 country code. He shrugged.
So, NetSuite experts, why doesn't Oracle allow for importing of customer data with two-letter ISO 3166 country code?
That's how system Architectured.
The only way is to use country enumerations table for lookup and replace codes with country names.
Ref: https://netsuite.custhelp.com/app/answers/detail/a_id/10888
For those of you who are hit by a password wall:
COUNTRY_MAPPINGS = {
"Afghanistan": "AF",
"Aland Islands": "AX",
"Albania": "AL",
"Algeria": "DZ",
"American Samoa": "AS",
"Andorra": "AD",
"Angola": "AO",
"Anguilla": "AI",
"Antarctica": "AQ",
"Antigua and Barbuda": "AG",
"Argentina": "AR",
"Armenia": "AM",
"Aruba": "AW",
"Australia": "AU",
"Austria": "AT",
"Azerbaijan": "AZ",
"Bahamas": "BS",
"Bahrain": "BH",
"Bangladesh": "BD",
"Barbados": "BB",
"Belarus": "BY",
"Belgium": "BE",
"Belize": "BZ",
"Benin": "BJ",
"Bermuda": "BM",
"Bhutan": "BT",
"Bolivia": "BO",
"Bonaire, Saint Eustatius, and Saba": "BQ",
"Bosnia and Herzegovina": "BA",
"Botswana": "BW",
"Bouvet Island": "BV",
"Brazil": "BR",
"British Indian Ocean Territory": "IO",
"Brunei Darussalam": "BN",
"Bulgaria": "BG",
"Burkina Faso": "BF",
"Burundi": "BI",
"Cambodia": "KH",
"Cameroon": "CM",
"Canada": "CA",
"Canary Islands": "IC",
"Cape Verde": "CV",
"Cayman Islands": "KY",
"Central African Republic": "CF",
"Ceuta and Melilla": "EA",
"Chad": "TD",
"Chile": "CL",
"China": "CN",
"Christmas Island": "CX",
"Cocos (Keeling) Islands": "CC",
"Colombia": "CO",
"Comoros": "KM",
"Congo, Democratic People's Republic": "CD",
"Congo, Republic of": "CG",
"Cook Islands": "CK",
"Costa Rica": "CR",
"Cote d'Ivoire": "CI",
"Croatia/Hrvatska": "HR",
"Cuba": "CU",
"Curacao": "CW",
"Cyprus": "CY",
"Czech Republic": "CZ",
"Denmark": "DK",
"Djibouti": "DJ",
"Dominica": "DM",
"Dominican Republic": "DO",
"East Timor": "TL",
"Ecuador": "EC",
"Egypt": "EG",
"El Salvador": "SV",
"Equatorial Guinea": "GQ",
"Eritrea": "ER",
"Estonia": "EE",
"Ethiopia": "ET",
"Falkland Islands": "FK",
"Faroe Islands": "FO",
"Fiji": "FJ",
"Finland": "FI",
"France": "FR",
"French Guiana": "GF",
"French Polynesia": "PF",
"French Southern Territories": "TF",
"Gabon": "GA",
"Gambia": "GM",
"Georgia": "GE",
"Germany": "DE",
"Ghana": "GH",
"Gibraltar": "GI",
"Greece": "GR",
"Greenland": "GL",
"Grenada": "GD",
"Guadeloupe": "GP",
"Guam": "GU",
"Guatemala": "GT",
"Guernsey": "GG",
"Guinea": "GN",
"Guinea-Bissau": "GW",
"Guyana": "GY",
"Haiti": "HT",
"Heard and McDonald Islands": "HM",
"Holy See (City Vatican State)": "VA",
"Honduras": "HN",
"Hong Kong": "HK",
"Hungary": "HU",
"Iceland": "IS",
"India": "IN",
"Indonesia": "ID",
"Iran (Islamic Republic of)": "IR",
"Iraq": "IQ",
"Ireland": "IE",
"Isle of Man": "IM",
"Israel": "IL",
"Italy": "IT",
"Jamaica": "JM",
"Japan": "JP",
"Jersey": "JE",
"Jordan": "JO",
"Kazakhstan": "KZ",
"Kenya": "KE",
"Kiribati": "KI",
"Korea, Democratic People's Republic": "KP",
"Korea, Republic of": "KR",
"Kosovo": "XK",
"Kuwait": "KW",
"Kyrgyzstan": "KG",
"Lao, People's Democratic Republic": "LA",
"Latvia": "LV",
"Lebanon": "LB",
"Lesotho": "LS",
"Liberia": "LR",
"Libya": "LY",
"Liechtenstein": "LI",
"Lithuania": "LT",
"Luxembourg": "LU",
"Macau": "MO",
"Macedonia": "MK",
"Madagascar": "MG",
"Malawi": "MW",
"Malaysia": "MY",
"Maldives": "MV",
"Mali": "ML",
"Malta": "MT",
"Marshall Islands": "MH",
"Martinique": "MQ",
"Mauritania": "MR",
"Mauritius": "MU",
"Mayotte": "YT",
"Mexico": "MX",
"Micronesia, Federal State of": "FM",
"Moldova, Republic of": "MD",
"Monaco": "MC",
"Mongolia": "MN",
"Montenegro": "ME",
"Montserrat": "MS",
"Morocco": "MA",
"Mozambique": "MZ",
"Myanmar": "MM",
"Namibia": "NA",
"Nauru": "NR",
"Nepal": "NP",
"Netherlands": "NL",
"New Caledonia": "NC",
"New Zealand": "NZ",
"Nicaragua": "NI",
"Niger": "NE",
"Nigeria": "NG",
"Niue": "NU",
"Norfolk Island": "NF",
"Northern Mariana Islands": "MP",
"Norway": "NO",
"Oman": "OM",
"Pakistan": "PK",
"Palau": "PW",
"Panama": "PA",
"Papua New Guinea": "PG",
"Paraguay": "PY",
"Peru": "PE",
"Philippines": "PH",
"Pitcairn Island": "PN",
"Poland": "PL",
"Portugal": "PT",
"Puerto Rico": "PR",
"Qatar": "QA",
"Reunion Island": "RE",
"Romania": "RO",
"Russian Federation": "RU",
"Rwanda": "RW",
"Saint Barthélemy": "BL",
"Saint Helena": "SH",
"Saint Kitts and Nevis": "KN",
"Saint Lucia": "LC",
"Saint Martin": "MF",
"Saint Vincent and the Grenadines": "VC",
"Samoa": "WS",
"San Marino": "SM",
"Sao Tome and Principe": "ST",
"Saudi Arabia": "SA",
"Senegal": "SN",
"Serbia": "RS",
"Seychelles": "SC",
"Sierra Leone": "SL",
"Singapore": "SG",
"Sint Maarten": "SX",
"Slovak Republic": "SK",
"Slovenia": "SI",
"Solomon Islands": "SB",
"Somalia": "SO",
"South Africa": "ZA",
"South Georgia": "GS",
"South Sudan": "SS",
"Spain": "ES",
"Sri Lanka": "LK",
"State of Palestine": "PS",
"St. Pierre and Miquelon": "PM",
"Sudan": "SD",
"Suriname": "SR",
"Svalbard and Jan Mayen Islands": "SJ",
"Swaziland": "SZ",
"Sweden": "SE",
"Switzerland": "CH",
"Syrian Arab Republic": "SY",
"Taiwan": "TW",
"Tajikistan": "TJ",
"Tanzania": "TZ",
"Thailand": "TH",
"Togo": "TG",
"Tokelau": "TK",
"Tonga": "TO",
"Trinidad and Tobago": "TT",
"Tunisia": "TN",
"Turkey": "TR",
"Turkmenistan": "TM",
"Turks and Caicos Islands": "TC",
"Tuvalu": "TV",
"Uganda": "UG",
"Ukraine": "UA",
"United Arab Emirates": "AE",
"United Kingdom": "GB",
"United States": "US",
"Uruguay": "UY",
"US Minor Outlying Islands": "UM",
"Uzbekistan": "UZ",
"Vanuatu": "VU",
"Venezuela": "VE",
"Vietnam": "VN",
"Virgin Islands, British": "VG",
"Virgin Islands, USA": "VI",
"Wallis and Futuna Islands": "WF",
"Western Sahara": "EH",
"Yemen": "YE",
"Zambia": "ZM",
"Zimbabwe": "ZW"
}
I understand spacy is parsing the given sentence and doing a POS tagging for the same.But after the sentence is parsed, i would like to get some sense of the output.
set an alarm for 7 PM tomorrow,
Expected output
{
Intent : set_alarm,
entity : { "time" : 7PM, "date": tomorrow}
}
Output from spacy :
[
{
word: "Set",
lemma: "set",
NE: "",
POS_fine: "JJ",
POS_coarse: "ADJ",
arc: "ROOT",
children: [
{
word: "alarm",
lemma: "alarm",
NE: "",
POS_fine: "NN",
POS_coarse: "NOUN",
arc: "dobj",
children: [ ]
},
{
word: "for",
lemma: "for",
NE: "",
POS_fine: "IN",
POS_coarse: "ADP",
arc: "prep",
children: [
{
word: "9 pm",
lemma: "9 pm",
NE: "TIME",
POS_fine: "NN",
POS_coarse: "NOUN",
arc: "pobj",
children: [ ]
}
]
},
{
word: "today",
lemma: "today",
NE: "",
POS_fine: "NN",
POS_coarse: "NOUN",
arc: "npadvmod",
children: [ ]
}
]
}
]
Your output is a parse tree. You're also given part of speech information (POS) and recognized named entities (NE). What you provide as expected output is called intent detection as far as I remember, please also see this ticket.