Question: Write a function to return the intersection of two arrays: For example, the intersection of [1, 2, 2, 1] and [2, 2] returns [2]. The return array should only contain unique numbers and the order does not matter.
Using STL, we can have two sets, one for recording the first array, the other for checking duplicates in the output array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Solution { public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { unordered_set<int> nums; for (auto x: nums1) { nums.insert(x); } vector<int> r; unordered_set<int> dup; for (auto x: nums2) { if (nums.count(x)) { if (!dup.count(x)) { // no duplicates r.push_back(x); } dup.insert(x); } } return r; } }; |
class Solution { public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { unordered_set<int> nums; for (auto x: nums1) { nums.insert(x); } vector<int> r; unordered_set<int> dup; for (auto x: nums2) { if (nums.count(x)) { if (!dup.count(x)) { // no duplicates r.push_back(x); } dup.insert(x); } } return r; } };
However, we can use only 1 set to solve this problem. Once a number is added to output, we remove this from the set so it won’t be added back again for future duplicate numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | class Solution { public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { unordered_set<int> nums; for (auto x: nums1) { nums.insert(x); } vector<int> r; for (auto x: nums2) { if (nums.count(x)) { r.push_back(x); nums.erase(x); // removed from set } } return r; } }; |
class Solution { public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { unordered_set<int> nums; for (auto x: nums1) { nums.insert(x); } vector<int> r; for (auto x: nums2) { if (nums.count(x)) { r.push_back(x); nums.erase(x); // removed from set } } return r; } };
–EOF (The Ultimate Computing & Technology Blog) —
GD Star Rating
loading...
268 wordsloading...
Last Post: Dynamic Programming - How many ways to connect the pipes?
Next Post: Writing Unit Tests in VBScript
how i want to move code using phyton to c++…..please help me……this is my code using phyton….and i wan to move from phyton to c++…
print “Welcome To The Resistor Calculator”
print “Our Group Members\nMuhammad Harith Bin Kamlyzan\nMuhammad Hisyam Bin Harmizon\nIkhwan Hakim Bin Mohd Asri\nShahrul Izwan Bin Kamal Akhball\nMuhammad Shahzlan Bin Mohd Nizam”
start = raw_input(“Press Enter To Start”)
first_colours = {“Black”,”Brown”,”Red”,”Orange”,”Yellow”,”Green”,”Blue”,”Purple”,”Grey”,”White”}
while True:
first_colour = raw_input(“\nPlease State The First Colour Band\nBlack\nBrown\nRed\nOrange\nYellow\nGreen\nBlue\nPurple\nGrey\nWhite\nYour Choice?\n”)
if first_colour == “Black”:
first_colour = 0
break
elif first_colour == “Brown”:
first_colour = 1
break
elif first_colour == “Red”:
first_colour = 2
break
elif first_colour == “Orange”:
first_colour = 3
break
elif first_colour == “Yellow”:
first_colour = 4
break
elif first_colour == “Green”:
first_colour = 5
break
elif first_colour == “Blue”:
first_colour = 6
break
elif first_colour == “Purple”:
first_colour = 7
break
elif first_colour == “Grey”:
first_colour = 8
break
elif first_colour == “White”:
first_colour = 9
break
else:
print “Invalid colour name”
second_colours = {“Black”,”Brown”,”Red”,”Orange”,”Yellow”,”Green”,”Blue”,”Purple”,”Grey”,”White”}
while True:
second_colour = raw_input(“\nPlease State The Second Colour Band\nBlack\nBrown\nRed\nOrange\nYellow\nGreen\nBlue\nPurple\nGrey\nWhite\nYour Choice?\n”)
if second_colour == “Black”:
second_colour = 0
break
elif second_colour == “Brown”:
second_colour = 1
break
elif second_colour == “Red”:
second_colour = 2
break
elif second_colour == “Orange”:
second_colour = 3
break
elif second_colour == “Yellow”:
second_colour = 4
break
elif second_colour == “Green”:
second_colour = 5
break
elif second_colour == “Blue”:
second_colour = 6
break
elif second_colour == “Purple”:
second_colour = 7
break
elif second_colour == “Grey”:
second_colour = 8
break
elif second_colour == “White”:
second_colour = 9
break
else:
print “Invalid colour name”
third_colours = {“Black”,”Brown”,”Red”,”Orange”,”Yellow”,”Green”,”Blue”,”Purple”,”Grey”,”White”}
while True:
third_colour = raw_input(“\nPlease State The Third Colour Band\nBlack\nBrown\nRed\nOrange\nYellow\nGreen\nBlue\nPurple\nGrey\nWhite\nYour Choice?\n”)
if third_colour == “Black”:
third_colour = 1
break
elif third_colour == “Brown”:
third_colour = 10
break
elif third_colour == “Red”:
third_colour = 100
break
elif third_colour == “Orange”:
third_colour = 1000
break
elif third_colour == “Yellow”:
third_colour = 10000
break
elif third_colour == “Green”:
third_colour = 100000
break
elif third_colour == “Blue”:
third_colour = 1000000
break
elif third_colour == “Purple”:
third_colour = 10000000
break
elif third_colour == “Grey”:
third_colour = 100000000
break
elif third_colour == “White”:
third_colour = 1000000000
break
else:
print “Invalid colour name”
fourth_colours = {“Gold”,”Silver”,”None”}
while True:
fourth_colour = raw_input(“\nPlease State The Fourth Colour Band\nGold\nSilver\nNone\nYour Choice?\n”)
if fourth_colour == “Gold”:
fourth_colour = 5
break
elif fourth_colour == “Silver”:
fourth_colour = 10
break
elif fourth_colour == “None”:
fourth_colour = 20
break
else:
print “Invalid colour name”
total = second_colour*third_colour
print “The Total Value Of Your Resistor Is : ” + str(first_colour) + str(total) + ” +-” + str(fourth_colour) + ‘%\n’
exitprogram = raw_input (“\nPress Enter To Exit”)
Project Title: Resistor Calculator Resistor is an electrical component that reduces an electric current. The resistor’s ability to reduce current is called resistance and is measured in units of ohms, Ω. The resistance of a resistor and its tolerance are marked on the resistor by color bands that denotes the resistance value as shown in Figure 1. Table 1 shows the value of each colour band.
Figure 1: Resistor colour band.
Table 1: Digit, multiplier and tolerance band chart. Colour 1st Band 2nd Band 3rd Band (Multiplier) 4th Band (Tolerance) Black 0 0 1 Brown 1 1 10 Red 2 2 100 Orange 3 3 1 000 Yellow 4 4 10 000 Green 5 5 100 000 Blue 6 6 1 000 000 Violet 7 7 10 000 000 Grey 8 8 100 000 000 White 9 9 1 000 000 000 Gold ±5% Silver ±10% None ±20%
You are required to design a program to determine the value of a resistor based on resistor colour code. The program should prompt users to request the 4 bands colour marked on the resistor. The result should be display along with its tolerance. The user then asked whether he/she want to continue to determine the value of other resistor or not. If the user enter “Y” or “y”, the program will continue, otherwise enter “N”or “n” the program will stop. The program also should prompt an error message if the user enter an invalid colour code. Bonus marks will be given to any group that can show the correct error message in each selection of colour code.
Example calculation By referring to Figure 1, the colour band of the resistor is as follow: 1st band : Red ——–> 2 2nd band : Yellow —-> 4 3rd band : Green —–> 100 000 4th band : Silver ——> ±10% Thus, its resistance value is : Formula : [( 10 x 1st band ) + 2nd band ] x 3rd band ) ± 4th band % Resistance = [ (10 x 2 + 4) x 100 000] ±10% = 2 400 000 Ω ±10%
please help me to finish this code
what have you done so far?
#include
#include
#include
#include
const char * colors[10] = { “black”, “brown”, “red”, “orange”, “yellow”,
“green”, “blue”, “violet”, “grey”, “white” };
int get_band (int n)
{ char input[10];
printf (“Enter color of band# %d: ” , n+1);
scanf (“%s”, input);
for (int i=0; i<10; i++)
{ if (strcmp(input, colors[i]) == 0)
return i;
}
printf ("Invalid input\n");
exit (1);
}
int main()
{ int band[3];
long value;
for (int i=0; i<3; i++)
band[i] = get_band(i);
value = band[0] * 10 + band[1];
value *= (long)pow(10.0, (double)band[2]);
printf ("The value of that resistor is %ld ohms\n", value);
system("pause");
return 0;
}
i don't know how to put a tolerance
can you help me to do a programing using c++ to determine the colour code of resistor
Is this an assignment? 🙂 send it to my address, and I will have a look, but no promise. dr.zhihua.lai [AT] gmail.com