convert code c++ to python online
#include <iostream> using namespace std; int main() { double IMC , masa , inaltime; cout << "Masa (kg): "; cin >> masa; cout << "Inaltime (m) "; cin >> inaltime; IMC = masa / (inaltime * inaltime); if (IMC < 18.5) cout << "Risc minim pentru sanatate" << endl; if (IMC >= 18.5 && IMC < 25) cout << "Risc minim/scazut pentru sanatate" << endl; if (IMC >= 25 && IMC < 30) cout << "Risc scazut/ moderat pentru sanatate" << endl; if (IMC >= 30 && IMC < 35) cout << "Risc moderat/ridicat pentru sanatate" << endl; if (IMC >= 35) cout << "Risc ridicat pentru sanatate" << endl; return 0; }
convert code c++ to python online
string solve(int n,int m){ string ans=""; vector <vector <int>> dp(n+2,vector <int> (2,0)); dp[n][0]=dp[n][1]=1; for(int i=n-1;i>0;i--){ dp[i][0]=dp[i+1][0]+dp[i+1][1]; dp[i][1]=dp[i+1][0]; } if(dp[1][0]+dp[1][1]<m)return "Not Possible"; for(int i=1;i<=n;i++){ if(dp[i][0]<m){ ans += 'B'; m -= dp[i][0]; } else { ans += 'A'; } } return ans; }
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 "DynamicIntArray.h" using namespace cs20a; DynamicIntArray::DynamicIntArray() { capacity = 2; used = 0; elements = new int[capacity]; } DynamicIntArray::DynamicIntArray(int size) { capacity = size; used = 0; elements = new int[capacity]; } DynamicIntArray::DynamicIntArray(const DynamicIntArray& d) { capacity = d.capacity; used = d.used; elements = new int[capacity]; for(int i = 0; i < used; i++) elements[i] = d.elements[i]; } DynamicIntArray::~DynamicIntArray() { capacity = 0; used = 0; delete elements; } DynamicIntArray& DynamicIntArray::operator=(const DynamicIntArray& rhs) { capacity = rhs.capacity; used = rhs.used; elements = new int[capacity]; for(int i = 0; i < used; i++) elements[i] = rhs.elements[i]; return *this; } bool DynamicIntArray::isEmpty() const { return used == 0; } int DynamicIntArray::getUsed() const { return used; } int DynamicIntArray::getCapacity() const { return capacity; } void DynamicIntArray::add(int element) { insert(used, element); } void DynamicIntArray::insert(int i, int element) { if(used == capacity) expandCapacity(); for(int j = used; j >= i; j--) elements[j] = elements[j-1]; elements[i] = element; } int DynamicIntArray::remove(int i) { int temp = elements[i]; for(int j = i; j < used-1; j++) elements[j] = elements[j+1]; return temp; } void DynamicIntArray::clear() { used = 0; } int& DynamicIntArray::operator[](int i) const { return elements[i]; } std::ostream& operator<<(std::ostream& outs, const DynamicIntArray& d) { for(int i = 0; i < d.getUsed(); i++) outs<<d[i]<<" "; return outs; } bool operator==(const DynamicIntArray& d1, const DynamicIntArray& d2) { if(d1.getCapacity() != d2.getCapacity()) return false; if(d1.getUsed() != d2.getUsed()) return false; for(int i = 0; i < d1.getUsed(); i++) if(d1[i] != d2[i]) return false; return true; } void DynamicIntArray::expandCapacity() { int newCapacity = capacity * 2; elements = (int*) realloc (elements, newCapacity * sizeof(int)); capacity = newCapacity; }
convert code c++ to python online
string solve(int n,int m){ string ans=""; vector <vector <int>> dp(n+2,vector <int> (2,0)); dp[n][0]=dp[n][1]=1; for(int i=n-1;i>0;i--){ dp[i][0]=dp[i+1][0]+dp[i+1][1]; dp[i][1]=dp[i+1][0]; } if(dp[1][0]+dp[1][1]<m)return "Not Possible"; for(int i=1;i<=n;i++){ if(dp[i][0]<m){ ans += 'B'; m -= dp[i][0]; } else { ans += 'A'; } } return ans; }
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 <iostream> using namespace std; int main() { int a;cin>>a; int n=0; int d[a*a]; for(int j=1;j<=a*a;j++){cin>>d[j];n+=d[j];} n/=a; n/=a; for(int j=1;j<=a*a;j++){ if(d[j]<n)cout<<0; else cout<<255; if(j%a==0)cout<<endl; else cout<<" "; } return 0; }
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
float area( int Npts, float X[], float Y[]) { float *DX = new float[Npts]; float *DY = new float[Npts]; float A = 0; for ( int i=0; i < Npts; i++) { DX[ i ] = (X[(i+1)%Npts] - X[(i+Npts-1)%Npts])/2; DY[ i ] = (Y[(i+1)%Npts] - Y[(i+Npts-1)%Npts])/2; } for ( int i=0; i < Npts; i++) A = A + (X[ i ]*DY[ i ] - Y[ i ]*DX[ i ]); delete [] DX; delete [] DY; return ( fabs(A/2) ); }
convert code c++ to python online
csacda