I have a vector Type which contains 100 values: [ 'City', 'Town', 'City', 'City',......, 'Town']
I want to associate/ map each of the string's in this vector with an integer/ double 10 and 20.
My attempt at the same:
int s = 20;
int o = 10;
for (int q = 0; q < 100; q++) {
if (Type[q] == 'City') {
'City' == s;
}
else (Type[q] == 'Town'){
'Town' == o;
}
}
This does not work. I would appreciate any help on the topic.
You can use std::map<std::string, int> (or std::map<std::string, double>) like this:
std::vector<std::string> Type = { "City", "Town", "City", "City", "Town" };
std::map<std::string, int> m;
int s = 20;
int o = 10;
for (size_t q = 0; q < Type.size(); q++) {
if (Type[q] == "City") {
m["City"] = s;
}
else if (Type[q] == "Town") {
m["Town"] = o;
}
}
You'll get a map with two values:
{ "City": 20 }, { "Town": 10 }
If you want to have pairs or type and number you can use vector of pairs or tuples:
std::vector<std::tuple<std::string, int>> tuples(Type.size());
int s = 20;
int o = 10;
for (size_t q = 0; q < Type.size(); q++) {
if (Type[q] == "City") {
tuples[q] = std::make_tuple("City", s);
}
else if (Type[q] == "Town") {
tuples[q] = std::make_tuple("Town", o);
}
}
You'll get a vector with values:
{"City", 20 }, {"Town", 10 }, { "City", 20 }, { "City", 20 }, {"Town", 10 }
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 15 hours ago.
Improve this question
I am trying to create a Struct with two structs inside. One struct has a 2d array in it. The other struct packs 2 versions of the previous struct. I have a working code. However, in this code I have this statement:
`if (lane_bank_select == 0) {
for (uint8_t i = 0; i < lane_amount; i = i + 1) {
Serial.print(db.data_lane_bank_1[i].data_1);
Serial.print(" ");
Serial.print(db.data_lane_bank_1[i].data_2);
Serial.print(" ");
Serial.print(db.data_lane_bank_1[i].data_3);
Serial.print(" ");
Serial.println(db.data_lane_bank_1[i].data_4);
}
} else if (lane_bank_select == 1) {
for (uint8_t i = 0; i < lane_amount; i = i + 1) {
Serial.print(db.data_lane_bank_2[i].data_1);
Serial.print(" ");
Serial.print(db.data_lane_bank_2[i].data_2);
Serial.print(" ");
Serial.print(db.data_lane_bank_2[i].data_3);
Serial.print(" ");
Serial.println(db.data_lane_bank_2[i].data_4);
}
} else {
Serial.print(db.data_lane_bank_1[0].data_1);
Serial.print(" ");
Serial.print(db.data_lane_bank_1[0].data_2);
Serial.print(" ");
Serial.print(db.data_lane_bank_1[0].data_3);
Serial.print(" ");
Serial.println(db.data_lane_bank_1[0].data_4);
}`
I would like to change this statement to something like this.
`for (uint8_t i = 0; i < memory_amount; i = i + 1) {
if (lane_bank_select == i) {
for (uint8_t j = 0; j < bank_amount; j = j + 1) {
for (uint8_t k = 0; k < lane_amount; k = k + 1) {
for (uint8_t l = 0; l < data_amount; l = l + 1) {
Serial.print(db.data_lane_bank[j][k].data[l]);
Serial.println(" ");
}
}
}
}
}`
So i wrote this:
`const uint8_t memory_amount = 2;
const uint8_t bank_amount = 2;
const uint8_t data_amount = 4;
const uint8_t lane_amount = 4;
const uint8_t lane_array_size = 4;
uint8_t lane_bank_select = 0; // This is used to select the data lane bank
struct data_lane { // creates 4 variables for the data lane.
uint8_t data[data_amount];
};
// packs 2 versions of data_lane struct into one struct
struct data_bank {
data_lane data_lane_bank[bank_amount][lane_amount];
};
void setup() {
Serial.begin(9600);
}
void loop() {
// use serial port to select data_lane bank via lane_bank_select
serial_lane_select();
data_bank db = {
.data_lane_bank[0] = {
{ 1, 2, 3, 4 }, // Data_lane_1
{ 11, 12, 13, 14 }, // Data_lane_2
{ 21, 22, 23, 24 }, // Data_lane_3
{ 31, 32, 33, 34 }, // Data_lane_4
},
.data_lane_bank[1] = {
{ 41, 42, 43, 44 }, // Data_lane_1
{ 51, 52, 53, 54 }, // Data_lane_2
{ 61, 62, 63, 64 }, // Data_lane_3
{ 71, 72, 73, 74 }, // Data_lane_4
}
};
for (uint8_t i = 0; i < memory_amount; i = i + 1) {
if (lane_bank_select == i) {
for (uint8_t j = 0; j < bank_amount; j = j + 1) {
for (uint8_t k = 0; k < lane_amount; k = k + 1) {
for (uint8_t l = 0; l < data_amount; l = l + 1) {
Serial.print(db.data_lane_bank[j][k].data[l]);
Serial.println(" ");
}
}
}
}
}
}
void serial_lane_select() {
uint8_t lane_recieved;
if (Serial.available() > 0) {
lane_recieved = Serial.read();
if (lane_recieved == '1') // Single Quote! This is a character.
{
lane_bank_select = 0;
}
if (lane_recieved == '2') {
lane_bank_select = 1;
}
}
}`
I am now getting this error:
expected primary-expression before '.' token .data_lane_bank[0] =
Can somebody help please.
Here is the problem link: https://leetcode.com/problems/roman-to-integer/
I kept having problem on comparison s.charAt() and a char of Roman.
I am very new to JAVA. Isn't if(s.charAt(i) == 'I') supposed to work?
I saw other solutions of this problem, but the solutions tend to make a char and put the char of string in that char.
Could you please explain why my comparison is not working?
I really appreciate your answer in advance.
class Solution {
public int romanToInt(String s) {
int i = 0;
int sum = 0;
for(i=0;i<s.length();i++)
{
if(s.charAt(i) == 'I')
{
if(s.charAt(i+1) == 'X') //4
{
i++;
sum += 4;
}
else if(s.charAt(i+1) == 'V') //9
{
i++;
sum += 9;
}
else
{
sum += 1;
}
}
else if(s.charAt(i) == 'V')
{
sum += 10;
}
else if(s.charAt(i) == 'X')
{
if(s.charAt(i+1) == 'L') //40
{
i++;
sum += 40;
}
else if(s.charAt(i+1) == 'C') //90
{
i++;
sum += 90;
}
else
{
sum += 10;
}
}
else if(s.charAt(i) == 'L')
{
sum += 50;
}
else if(s.charAt(i) == 'C')
{
if(s.charAt(i+1) == 'D') //400
{
i++;
sum += 400;
}
else if(s.charAt(i+1) == 'M') //900
{
i++;
sum += 900;
}
else
{
sum+=100;
}
}
else if(s.charAt(i) == 'D')
{
sum += 500;
}
else if(s.charAt(i) == 'M')
{
sum += 1000;
}
}
return sum;
}
}
I used Java for this:
public int romanToInt(String s) {
HashMap<Character, Integer> dict = new HashMap<Character, Integer>();
{
dict.put('I', 1);
dict.put('V', 5);
dict.put('X', 10);
dict.put('L', 50);
dict.put('C', 100);
dict.put('D', 500);
dict.put('M', 1000);
}
int temp = 0;
for(int i=0; i<s.length(); i++){
char c= s.charAt(i);
temp += dict.get(c);
char d= '.';
if((i+1)<s.length() && d!=c){
d = s.charAt(i+1);
if((c=='I' && (d=='V' || d=='X')) ||
(c=='X' && (d=='L' || d=='C')) ||
(c=='C' && (d=='D' || d=='M')))
{
temp += dict.get(d)-2*dict.get(c);
i++;
}
}
}
return temp;
}
Hi I was trying to solve the interleaving strings problem.Here is the detailed explanation of the problem. https://practice.geeksforgeeks.org/problems/interleaved-strings/1
I was trying using lcs but it was not passing leetcode cases. Here is my Code:-
(I am taking lcs from start and end)
class Solution {
public boolean isInterLeave(String a, String b, String c) {
StringBuffer s=new StringBuffer();
StringBuffer s1=new StringBuffer();
StringBuffer s2=new StringBuffer();
StringBuffer s4=new StringBuffer();
int m=a.length();
int n=c.length();
int q=b.length();
if(n!=m+q){
return false;
}
LinkedHashSet<Integer> res2= new LinkedHashSet<Integer>();
res2= lcs(a,c,m,n);
LinkedHashSet<Integer> res4= new LinkedHashSet<Integer>();
res4= lcs(b,c,q,n);
for(int i=0;i<n;i++){
if(res2.contains(i)==false){
s.append(c.charAt(i));
}
}
for(int i=0;i<n;i++){
if(res4.contains(i)==false){
s1.append(c.charAt(i));
}
}
LinkedHashSet<Integer> res5= new LinkedHashSet<Integer>();
res5= LCS(a,c,m,n);
for(int i=0;i<n;i++){
if(res5.contains(i)==false){
s2.append(c.charAt(i));
}
} LinkedHashSet<Integer> res6= new LinkedHashSet<Integer>();
res6= LCS(b,c,q,n);
for(int i=0;i<n;i++){
if(res6.contains(i)==false){
s4.append(c.charAt(i));
}
}
String z=s.toString();
String u=s1.toString();
String v=s2.toString();
String w=s4.toString();
if( (b.equals(z)==true || a.equals(u)==true) || ( b.equals(v)==true || a.equals(w)==true)){
return true;
}
else{
return false;
}
}
public static LinkedHashSet<Integer> lcs(String X, String Y, int m, int n)
{
int[][] L = new int[m+1][n+1];
// Following steps build L[m+1][n+1] in bottom up fashion. Note
// that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1]
for (int i=0; i<=m; i++)
{
for (int j=0; j<=n; j++)
{
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X.charAt(i-1) == Y.charAt(j-1))
L[i][j] = L[i-1][j-1] + 1;
else
L[i][j] = Math.max(L[i-1][j], L[i][j-1]);
}
}
// Following code is used to print LCS
// Create a character array to store the lcs string
LinkedHashSet<Integer> linkedset =
new LinkedHashSet<Integer>();
// Start from the right-most-bottom-most corner and
// one by one store characters in lcs[]
int i=1;
int j=1;
while (i <= m && j <= n)
{
// If current character in X[] and Y are same, then
// current character is part of LCS
if (X.charAt(i-1) == Y.charAt(j-1))
{
// Put current character in result
linkedset.add(j-1);
// reduce values of i, j and index
i++;
j++;
}
// If not same, then find the larger of two and
// go in the direction of larger value
else if (L[i-1][j] > L[i][j-1])
i++;
else
j++;
}
return linkedset;
}
public static LinkedHashSet<Integer> LCS(String X, String Y, int m, int n)
{
int[][] L = new int[m+1][n+1];
// Following steps build L[m+1][n+1] in bottom up fashion. Note
// that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1]
for (int i=0; i<=m; i++)
{
for (int j=0; j<=n; j++)
{
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X.charAt(i-1) == Y.charAt(j-1))
L[i][j] = L[i-1][j-1] + 1;
else
L[i][j] = Math.max(L[i-1][j], L[i][j-1]);
}
}
// Following code is used to print LCS
// Create a character array to store the lcs string
LinkedHashSet<Integer> linkedset =
new LinkedHashSet<Integer>();
// Start from the right-most-bottom-most corner and
// one by one store characters in lcs[]
int i = m;
int j = n;
while (i > 0 && j > 0)
{
// If current character in X[] and Y are same, then
// current character is part of LCS
if (X.charAt(i-1) == Y.charAt(j-1))
{
// Put current character in result
linkedset.add(j-1);
// reduce values of i, j and index
i--;
j--;
}
// If not same, then find the larger of two and
// go in the direction of larger value
else if (L[i-1][j] > L[i][j-1])
i--;
else
j--;
}
return linkedset;
}
}
Can anyone suggest an LCS approach to this problem?.My code is not passing the following test case
"cacabcbaccbbcbb" -String A
"acaaccaacbbbabbacc"-String B
"accacaabcbacaccacacbbbbcbabbbbacc"-String C
This will be the LCS+DP approach. Try it out:
class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
int m = s1.length(), n = s2.length();
if (n + m != s3.length()) return false;
if (s3.length() == 0) return true;
boolean[][] dp = new boolean[m+1][n+1];
dp[0][0] = true;
for (int i = 0; i <= m; i++) {
if (s1.substring(0, i).equals(s3.substring(0, i)))
dp[i][0] = true;
else
dp[i][0] = false;
}
for (int j = 0; j <= n; j++) {
if (s2.substring(0, j).equals(s3.substring(0, j)))
dp[0][j] = true;
else
dp[0][j] = false;
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j] = (dp[i-1][j] && s1.charAt(i-1) == s3.charAt(i+j-1))
|| (dp[i][j-1] && s2.charAt(j-1) == s3.charAt(i+j-1));
}
}
return dp[m][n];
}
}
I have two functions recursive and iterative to calculate money change; in the iterative version I needed to check for if the money is multiple of change (money modulus change is zero): is there a way to do iterative without checking for modulus?
public static int MoneyChangeRecur(int money, int[] changes)
{
int minChange = 0;
if (money == 0) return 0;
int min = int.MaxValue;
for (int i = 0; i < changes.Length; i++)
{
if (money >= changes[i])
{
minChange = MoneyChangeRecur(money - changes[i], changes);
if (minChange + 1 <= min)
{
min = minChange + 1;
}
}
}
return min;
}
static int IterativeMoneychange(int money)
{
int[] ar = new int[money + 1];
int[] change = { 6, 5, 1 };
int min = 9999;
int index = 0;
for (int i = 0; i < money+1; i++)
{
min = 99999;
index = 0;
bool modSet = false;
for (int j= 0; j <change.Length; j++)
{
if (i >= change[j])
{
int mod=(i % change[j]);
if(mod==0&&change[j]!=1)
{
if (!modSet) min = 99999;
if ((i-change[j] )< min)
{
min = (i - change[j]);
modSet = true;
}
}
else
{
if ((i - change[j]) < min)
{
min = (i - change[j]);
modSet = false;
}
}
}
}
if (min != 99999)// min = 0;
ar[i] = ar[min] +1;
}
return ar[money];
}enter code here
I found a project description on a course website for computer graphics. I am trying to complete the project for fun.
Here is the link to the problem description:
http://www.pdfhost.net/index.php?Action=Download&File=901bc7785bef41364b3a40f6f4493926
Below is my code. The problem I am running in to is that the terms of the series grow so fast I can't map the points to the screen correctly. From the problem description it says the points will be mappable within a -2 - 2 square but the difference in value between the points is so huge that normalizing by the largest would collapse most of the points to a single pixel.
I assume I have a fundamental misunderstanding that I can't identify. Any help or insight would be appreciated!
int w = 800, h = 600;
int numTimes = 10, cSize = 5;
float xr = 2, yr = 2;
void setup() {
size(w,h);
}
void draw() {
background(255);
Complex v = new Complex(mouseX*(xr/w) - (xr/2), mouseY*(yr/h) - (yr/2));
Complex[] exps = new Complex[numTimes];
for (int i = 0; i < numTimes; i++) {
exps[i] = complexExp(v,i);
}
ellipse(w/2, h/2, cSize, cSize);
for (int i = 0; i < numTimes; i++) {
drawSeries(new Complex(0,0), exps, i, i);
}
}
void drawSeries(Complex vToDraw, Complex[] exps, int count, int clrTrunc) {
if (count == 0) {
Complex v = exps[0];
float progress = float(clrTrunc) / float(numTimes);
fill(255*progress, 180, 255 - 255*progress);
vToDraw.add(v);
ellipse(vToDraw.r*(w/xr) + (w/2), vToDraw.i*(h/xr) + h/2, cSize, cSize);
vToDraw.sub(v);
vToDraw.sub(v);
ellipse(vToDraw.r*(w/xr) + (w/2), vToDraw.i*(h/xr) + h/2, cSize, cSize);
} else {
Complex v = exps[count];
vToDraw.add(v);
drawSeries(vToDraw, exps, count - 1, clrTrunc );
vToDraw.sub(v);
vToDraw.sub(v);
drawSeries(vToDraw, exps, count - 1,clrTrunc );
}
}
Complex complexExp(Complex v, int times) {
if (times == 0) {
return new Complex(1, 1);
} else if ( times == 1) {
return new Complex( v.r*v.r - v.i*v.i, 2*v.r*v.i );
} else {
return complexExp( new Complex( v.r*v.r - v.i*v.i, 2*v.r*v.i ), times - 1 );
}
}
class Complex {
float r, i;
Complex() {
this.r = 0;
this.i = 0;
}
Complex(float r, float i) {
this.r = r;
this.i = i;
}
void add(Complex nv) {
this.r += nv.r;
this.i += nv.i;
}
void sub(Complex nv) {
this.r -= nv.r;
this.i -= nv.i;
}
}
I think you can make the code cleaner if you write a more complete Complex class.
int w = 800, h = 600;
int numTimes = 10, cSize = 5;
float xr = 3, yr = 3;
void setup() {
size(w,h);
noLoop();
}
void mousePressed() {
redraw();
}
void draw() {
background(255);
Complex v = new Complex(mouseX*(xr/w) - (xr/2), mouseY*(yr/h) - (yr/2));
Complex[] exps = new Complex[numTimes];
for (int i = 0; i < numTimes; i++) {
exps[i] = v.raisedTo(i);
print(exps[i]);
}
ellipse(w/2, h/2, cSize, cSize);
print(exps);
drawSerie(exps, numTimes);
}
void drawSerie(Complex[] exps, int total)
{
Complex partial = new Complex(0, 0);
drawPartial(exps, total -1, partial);
}
void drawFinal(Complex toDraw)
{
point(toDraw.r*(w/xr) + (w/2), toDraw.i*(h/xr) + h/2);
}
void drawPartial(Complex [] exps, int depth, Complex partial)
{
if (depth == -1)
{
drawFinal(partial);
return;
}
int nextDepth = depth -1;
drawPartial(exps, nextDepth, partial);
Complex element = exps[depth];
drawPartial(exps, nextDepth, partial.add(element));
drawPartial(exps, nextDepth, partial.sub(element));
}
class Complex {
float r, i;
Complex() {
this.r = 0;
this.i = 0;
}
Complex(float r, float i) {
this.r = r;
this.i = i;
}
Complex(Complex other)
{
this.r = other.r;
this.i = other.i;
}
Complex mult(Complex other)
{
return new Complex(this.r*other.r - this.i*other.i, this.r*other.i + this.i*other.r);
}
Complex add(Complex nv) {
return new Complex(this.r + nv.r, this.i + nv.i);
}
Complex sub(Complex nv) {
return new Complex(this.r - nv.r, this.i - nv.i);
}
Complex raisedTo(int n) {
if (n == 0) {
return new Complex(1, 0);
}
else if (n % 2 == 0)
{
return (this.mult(this)).raisedTo(n/2);
}
else
{
return this.mult(this.raisedTo(n - 1 ));
}
}
String toString()
{
return "real: " + this.r + " imaginary: " + this.i;
}
}
The computation of the series is not efficient but, I think, it is clear