convert code c++ to python online
csacda
convert code c++ to python online
// Program to print path from root node to destination node
// for N*N -1 puzzle algorithm using Branch and Bound
// The solution assumes that instance of puzzle is solvable
#include <bits/stdc++.h>
using namespace std;
#define N 3
// state space tree nodes
struct Node
{
// stores the parent node of the current node
// helps in tracing path when the answer is found
Node* parent;
// stores matrix
int mat[N][N];
// stores blank tile coordinates
int x, y;
// stores the number of misplaced tiles
int cost;
// stores the number of moves so far
int level;
};
// Function to print N x N matrix
int printMatrix(int mat[N][N])
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
printf("%d ", mat[i][j]);
printf("\n");
}
}
// Function to allocate a new node
Node* newNode(int mat[N][N], int x, int y, int newX,
int newY, int level, Node* parent)
{
Node* node = new Node;
// set pointer for path to root
node->parent = parent;
// copy data from parent node to current node
memcpy(node->mat, mat, sizeof node->mat);
// move tile by 1 position
swap(node->mat[x][y], node->mat[newX][newY]);
// set number of misplaced tiles
node->cost = INT_MAX;
// set number of moves so far
node->level = level;
// update new blank tile cordinates
node->x = newX;
node->y = newY;
return node;
}
// bottom, left, top, right
int row[] = { 1, 0, -1, 0 };
int col[] = { 0, -1, 0, 1 };
// Function to calculate the number of misplaced tiles
// ie. number of non-blank tiles not in their goal position
int calculateCost(int initial[N][N], int final[N][N])
{
int count = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (initial[i][j] && initial[i][j] != final[i][j])
count++;
return count;
}
// Function to check if (x, y) is a valid matrix cordinate
int isSafe(int x, int y)
{
return (x >= 0 && x < N && y >= 0 && y < N);
}
// print path from root node to destination node
void printPath(Node* root)
{
if (root == NULL)
return;
printPath(root->parent);
printMatrix(root->mat);
printf("\n");
}
// Comparison object to be used to order the heap
struct comp
{
bool operator()(const Node* lhs, const Node* rhs) const
{
return (lhs->cost + lhs->level) > (rhs->cost + rhs->level);
}
};
// Function to solve N*N - 1 puzzle algorithm using
// Branch and Bound. x and y are blank tile coordinates
// in initial state
void solve(int initial[N][N], int x, int y,
int final[N][N])
{
// Create a priority queue to store live nodes of
// search tree;
priority_queue<Node*, std::vector<Node*>, comp> pq;
// create a root node and calculate its cost
Node* root = newNode(initial, x, y, x, y, 0, NULL);
root->cost = calculateCost(initial, final);
// Add root to list of live nodes;
pq.push(root);
// Finds a live node with least cost,
// add its childrens to list of live nodes and
// finally deletes it from the list.
while (!pq.empty())
{
// Find a live node with least estimated cost
Node* min = pq.top();
// The found node is deleted from the list of
// live nodes
pq.pop();
// if min is an answer node
if (min->cost == 0)
{
// print the path from root to destination;
printPath(min);
return;
}
// do for each child of min
// max 4 children for a node
for (int i = 0; i < 4; i++)
{
if (isSafe(min->x + row[i], min->y + col[i]))
{
// create a child node and calculate
// its cost
Node* child = newNode(min->mat, min->x,
min->y, min->x + row[i],
min->y + col[i],
min->level + 1, min);
child->cost = calculateCost(child->mat, final);
// Add child to list of live nodes
pq.push(child);
}
}
}
}
// Driver code
int main()
{
// Initial configuration
// Value 0 is used for empty space
int initial[N][N] =
{
{1, 2, 3},
{5, 6, 0},
{7, 8, 4}
};
// Solvable Final configuration
// Value 0 is used for empty space
int final[N][N] =
{
{1, 2, 3},
{5, 8, 6},
{0, 7, 4}
};
// Blank tile coordinates in initial
// configuration
int x = 1, y = 2;
solve(initial, x, y, final);
return 0;
}
convert code c++ to python online
#include <SoftwareSerial.h>
String responseData;
String message = "";
String senderNumber;
int pump = 0;
int temp = 0;
int i = 0;
int j = 0;
int k = 0;
int X = 0;
int Y = 0;
int mtr_on = 0;
bool pumpStatus=1;
const String phone="+919524261484";
SoftwareSerial gsmSerial(3,4);
float Time = 0,frequency = 0;
const int input = 2;
const int test = 6;
char str[15];
void setup(){
responseData.reserve(200);
phone.reserve(20);
pinMode(pump,OUTPUT);
digitalWrite(pump,HIGH);
gsmSerial.begin(9600);
analogWrite(test, 100);
gsmSerial.write("AT\r");
gsmSerial.write("AT+IPR=9600\r");
gsmSerial.print("AT+CMGF=1\r");
gsmSerial.print("AT+CNMI=2,2,0,0,0\r");
gsmSerial.print(gsmSerial.readString());
gsmSerial.write("AT+CLIP=1\r\n");
gsmSerial.println("System is ready");
}
void receive_message()
{
if (gsmSerial.available()>0)
{
responseData = gsmSerial.readStringUntil('\n');
gsmSerial.println(responseData);
parse();
delayMicroseconds(10);
}
{
if (temp == 1)
{
temp = 0;
i = 0;
delayMicroseconds(1000);
}
if (mtr_on == 1)
{
X = pulseIn(input, LOW);
Y = pulseIn(input, HIGH);
Time = X + Y;
frequency = 1000000 / Time;
if (isinf(frequency))
{
digitalWrite(pump, HIGH);
message = "pump Deactivated. Dry Run Shut Off!";
send_message(message);
gsmSerial.write(0x1A);
mtr_on = 0;
delayMicroseconds(1000);
}
}
}
}
void parse(){
if (responseData.indexOf("CLIP:")>0)
{
senderNumber=responseData.substring(responseData.indexOf("+CLIP: ") +8,responseData.indexOf("+CLIP: ") +21); //PARSE CALLER ID
gsmSerial.println("Caller number :");
gsmSerial.println(senderNumber);
if (senderNumber == phone)
{
gsmSerial.println("Sender number White list : ok");
pumpStatus=!pumpStatus;
digitalWrite(pump,pumpStatus);
gsmSerial.write("ATH\r");
gsmSerial.print("AT+CMGS=\""+phone+"\"\r");
delayMicroseconds(1000);
gsmSerial.print("0-activated 1-deactivated pump working-");
gsmSerial.print(pumpStatus);
delayMicroseconds(200);
gsmSerial.write(0x1A);
delayMicroseconds(100);
for (j = 0; j < 20 ; j++)
{
delayMicroseconds(1000);
}
mtr_on = 1;
}
gsmSerial.write("ATH\r");
delayMicroseconds(500);
}
}
void send_message(String message)
{
gsmSerial.println("AT+CMGF=1");
gsmSerial.println("AT+CMGS=\"+919524261484\"");
gsmSerial.println(message);
gsmSerial.write(0x1A);
delayMicroseconds(100);
}
void loop()
{
receive_message();
if(responseData.indexOf("pump on")>=0)
{
digitalWrite(pump, LOW);
message = "pump Activated";
send_message(message);
gsmSerial.write(0x1A);
for (j = 0; j < 20 ; j++)
{
delayMicroseconds(1000);
}
mtr_on = 1;
}
if(responseData.indexOf("pump off")>=0)
{
digitalWrite(pump, HIGH);
message = "pump Deactivated";
send_message(message);
gsmSerial.write(0x1A);
}
}
convert code c++ to python online
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string s;
cin >> s;
int t;
cin >> t;
for (int i = 0; i < t; i++) {
int a, b;
cin >> a >> b;
reverse(s.begin() + a, s.begin() + b + 1);
cout << s << endl;
}
return 0;
}
convert code c++ to python online
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long int i,j,n;
long long int x;
long long int m=1000001;
//generate prime numbers
vector<bool> primes(1000001,true);
set<long long int> st;
primes[0]=primes[1]=false;
//seive of eratosthenes
for(i=2;i*i<=m;i++)
{
if(primes[i])
{
for(j=i;i*j<=m;j++)
{
primes[i*j]=false;
}
}
}
//put all the t_primes in the set
for(i=2;i<=m;i++)
{
if(primes[i])
st.insert(i*i);
}
cout<<"How many numbers ?"<<endl;
cin>>n;
while(n--)
{
cout<<endl<<"Enter the number "<<endl;
cin>>x;
if(st.find(x)!=st.end())
cout<<x<<" is T-prime";
else
cout<<x<<" is NOT T-prime";
cout<<endl;
}
return 0;
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us