convert code c 2b 2b to python online

Solutions on MaxInterview for convert code c 2b 2b to python online by the best coders in the world

showing results for - "convert code c 2b 2b to python online"
Alina
18 Apr 2018
1// Program to print path from root node to destination node
2// for N*N -1 puzzle algorithm using Branch and Bound
3// The solution assumes that instance of puzzle is solvable
4#include <bits/stdc++.h>
5using namespace std;
6#define N 3
7
8// state space tree nodes
9struct Node
10{
11	// stores the parent node of the current node
12	// helps in tracing path when the answer is found
13	Node* parent;
14
15	// stores matrix
16	int mat[N][N];
17
18	// stores blank tile coordinates
19	int x, y;
20
21	// stores the number of misplaced tiles
22	int cost;
23
24	// stores the number of moves so far
25	int level;
26};
27
28// Function to print N x N matrix
29int printMatrix(int mat[N][N])
30{
31	for (int i = 0; i < N; i++)
32	{
33		for (int j = 0; j < N; j++)
34			printf("%d ", mat[i][j]);
35		printf("\n");
36	}
37}
38
39// Function to allocate a new node
40Node* newNode(int mat[N][N], int x, int y, int newX,
41			int newY, int level, Node* parent)
42{
43	Node* node = new Node;
44
45	// set pointer for path to root
46	node->parent = parent;
47
48	// copy data from parent node to current node
49	memcpy(node->mat, mat, sizeof node->mat);
50
51	// move tile by 1 position
52	swap(node->mat[x][y], node->mat[newX][newY]);
53
54	// set number of misplaced tiles
55	node->cost = INT_MAX;
56
57	// set number of moves so far
58	node->level = level;
59
60	// update new blank tile cordinates
61	node->x = newX;
62	node->y = newY;
63
64	return node;
65}
66
67// bottom, left, top, right
68int row[] = { 1, 0, -1, 0 };
69int col[] = { 0, -1, 0, 1 };
70
71// Function to calculate the number of misplaced tiles
72// ie. number of non-blank tiles not in their goal position
73int calculateCost(int initial[N][N], int final[N][N])
74{
75	int count = 0;
76	for (int i = 0; i < N; i++)
77	for (int j = 0; j < N; j++)
78		if (initial[i][j] && initial[i][j] != final[i][j])
79		count++;
80	return count;
81}
82
83// Function to check if (x, y) is a valid matrix cordinate
84int isSafe(int x, int y)
85{
86	return (x >= 0 && x < N && y >= 0 && y < N);
87}
88
89// print path from root node to destination node
90void printPath(Node* root)
91{
92	if (root == NULL)
93		return;
94	printPath(root->parent);
95	printMatrix(root->mat);
96
97	printf("\n");
98}
99
100// Comparison object to be used to order the heap
101struct comp
102{
103	bool operator()(const Node* lhs, const Node* rhs) const
104	{
105		return (lhs->cost + lhs->level) > (rhs->cost + rhs->level);
106	}
107};
108
109// Function to solve N*N - 1 puzzle algorithm using
110// Branch and Bound. x and y are blank tile coordinates
111// in initial state
112void solve(int initial[N][N], int x, int y,
113		int final[N][N])
114{
115	// Create a priority queue to store live nodes of
116	// search tree;
117	priority_queue<Node*, std::vector<Node*>, comp> pq;
118
119	// create a root node and calculate its cost
120	Node* root = newNode(initial, x, y, x, y, 0, NULL);
121	root->cost = calculateCost(initial, final);
122
123	// Add root to list of live nodes;
124	pq.push(root);
125
126	// Finds a live node with least cost,
127	// add its childrens to list of live nodes and
128	// finally deletes it from the list.
129	while (!pq.empty())
130	{
131		// Find a live node with least estimated cost
132		Node* min = pq.top();
133
134		// The found node is deleted from the list of
135		// live nodes
136		pq.pop();
137
138		// if min is an answer node
139		if (min->cost == 0)
140		{
141			// print the path from root to destination;
142			printPath(min);
143			return;
144		}
145
146		// do for each child of min
147		// max 4 children for a node
148		for (int i = 0; i < 4; i++)
149		{
150			if (isSafe(min->x + row[i], min->y + col[i]))
151			{
152				// create a child node and calculate
153				// its cost
154				Node* child = newNode(min->mat, min->x,
155							min->y, min->x + row[i],
156							min->y + col[i],
157							min->level + 1, min);
158				child->cost = calculateCost(child->mat, final);
159
160				// Add child to list of live nodes
161				pq.push(child);
162			}
163		}
164	}
165}
166
167// Driver code
168int main()
169{
170	// Initial configuration
171	// Value 0 is used for empty space
172	int initial[N][N] =
173	{
174		{1, 2, 3},
175		{5, 6, 0},
176		{7, 8, 4}
177	};
178
179	// Solvable Final configuration
180	// Value 0 is used for empty space
181	int final[N][N] =
182	{
183		{1, 2, 3},
184		{5, 8, 6},
185		{0, 7, 4}
186	};
187
188	// Blank tile coordinates in initial
189	// configuration
190	int x = 1, y = 2;
191
192	solve(initial, x, y, final);
193
194	return 0;
195}
196
María Fernanda
09 May 2017
1#include <SoftwareSerial.h>
2String responseData;   
3String message = "";  
4String senderNumber;
5int pump = 0;
6int temp = 0;
7int i = 0;
8int j = 0;
9int k = 0;
10int X = 0;
11int Y = 0;
12int mtr_on = 0; 
13bool pumpStatus=1;
14const String phone="+919524261484"; 
15SoftwareSerial gsmSerial(3,4);
16float Time = 0,frequency = 0;
17const int input = 2;
18const int test = 6;
19char str[15];
20void setup(){
21  responseData.reserve(200);
22  phone.reserve(20);
23  pinMode(pump,OUTPUT);
24  digitalWrite(pump,HIGH);
25  gsmSerial.begin(9600);
26  analogWrite(test, 100);
27  gsmSerial.write("AT\r");
28  gsmSerial.write("AT+IPR=9600\r");
29  gsmSerial.print("AT+CMGF=1\r");  
30  gsmSerial.print("AT+CNMI=2,2,0,0,0\r"); 
31  gsmSerial.print(gsmSerial.readString());
32  gsmSerial.write("AT+CLIP=1\r\n");
33gsmSerial.println("System is ready");
34}
35void receive_message()
36{
37  if (gsmSerial.available()>0)
38  {
39    responseData = gsmSerial.readStringUntil('\n');
40    gsmSerial.println(responseData);
41    parse();
42    delayMicroseconds(10);
43  }
44    {
45if (temp == 1)
46{
47temp = 0;
48i = 0;
49delayMicroseconds(1000);
50}
51if (mtr_on == 1)
52{
53X = pulseIn(input, LOW);
54Y = pulseIn(input, HIGH);
55Time = X + Y;
56frequency = 1000000 / Time;
57if (isinf(frequency))
58{
59digitalWrite(pump, HIGH);
60    message = "pump Deactivated. Dry Run Shut Off!";
61    send_message(message);
62    gsmSerial.write(0x1A); 
63mtr_on = 0;
64delayMicroseconds(1000);
65}
66}
67}
68}
69void parse(){
70  if (responseData.indexOf("CLIP:")>0)
71  {
72  senderNumber=responseData.substring(responseData.indexOf("+CLIP: ") +8,responseData.indexOf("+CLIP: ") +21); //PARSE CALLER ID 
73  gsmSerial.println("Caller number   :");
74  gsmSerial.println(senderNumber);
75   if (senderNumber == phone)
76   {
77    gsmSerial.println("Sender number White list : ok");
78    pumpStatus=!pumpStatus;
79         digitalWrite(pump,pumpStatus);
80    gsmSerial.write("ATH\r"); 
81    gsmSerial.print("AT+CMGS=\""+phone+"\"\r");
82    delayMicroseconds(1000);
83    gsmSerial.print("0-activated 1-deactivated pump working-");
84    gsmSerial.print(pumpStatus);
85    delayMicroseconds(200);
86    gsmSerial.write(0x1A); 
87    delayMicroseconds(100);
88     for (j = 0; j < 20 ; j++)
89{
90delayMicroseconds(1000);
91}
92mtr_on = 1;
93     }
94    gsmSerial.write("ATH\r");
95    delayMicroseconds(500);
96  }
97}
98void send_message(String message)
99{
100  gsmSerial.println("AT+CMGF=1");    
101  gsmSerial.println("AT+CMGS=\"+919524261484\""); 
102  gsmSerial.println(message);  
103    gsmSerial.write(0x1A); 
104   delayMicroseconds(100); 
105}
106void loop()
107{
108  receive_message();
109  if(responseData.indexOf("pump on")>=0)
110  {
111    digitalWrite(pump, LOW);
112    message = "pump Activated";
113    send_message(message);
114    gsmSerial.write(0x1A); 
115    for (j = 0; j < 20 ; j++)
116{
117delayMicroseconds(1000);
118}
119mtr_on = 1;
120}
121  if(responseData.indexOf("pump off")>=0)
122  {
123    digitalWrite(pump, HIGH);
124    message = "pump Deactivated";
125     send_message(message);
126    gsmSerial.write(0x1A);
127  }        
128}
Alan
22 Apr 2019
1#include <cmath>
2#include <cstdio>
3#include <vector>
4#include <iostream>
5#include <algorithm>
6using namespace std;
7
8
9int main() {
10    string s;
11    cin >> s;
12    int t;
13    cin >> t;
14    for (int i = 0; i < t; i++) {
15        int a, b;
16        cin >> a >> b;
17        reverse(s.begin() + a, s.begin() + b + 1);
18        cout << s << endl;
19    }
20    return 0;
21}
Chiara
26 Aug 2017
1csacda
Malia
21 Mar 2017
1#include<bits/stdc++.h>
2using namespace std;
3 
4int main()
5{
6    long long int i,j,n;
7    long long int x;
8    long long int m=1000001;
9 
10    //generate prime numbers 
11    vector<bool> primes(1000001,true);
12    set<long long int> st;
13    primes[0]=primes[1]=false;
14 
15    //seive of eratosthenes
16    for(i=2;i*i<=m;i++)
17    {
18        if(primes[i])
19        {
20            for(j=i;i*j<=m;j++)
21            {
22                primes[i*j]=false;
23            }
24        }
25    }
26 
27    //put all the t_primes in the set
28    for(i=2;i<=m;i++)
29    {
30        if(primes[i])
31            st.insert(i*i);
32    }
33 
34    cout<<"How many numbers ?"<<endl;
35    cin>>n;
36 
37    while(n--)
38    {
39        cout<<endl<<"Enter the number "<<endl;
40        cin>>x;
41 
42        if(st.find(x)!=st.end())
43            cout<<x<<" is T-prime";
44 
45        else
46            cout<<x<<" is NOT T-prime";
47 
48        cout<<endl;
49    }
50 
51    return 0;
52}