14,241,831 members
Rate this:
See more:
```def readinput():
n = int(input())     # Length
for j in range(n):
nextnum = int(input())  # Read each value
insequence.append(nextnum)
best.append(0)     # Initialize best[k] for each position
return

def solve():
for j in range(len(insequence)):
# Collect best[k] for values to left of j that divide insequence[j]
prev = [ best[k] for k in range(j) if insequence[j]%insequence[k] == 0 ]
if prev:
best[j] = 1 + max(prev)
else:
best[j] = 1

insequence = []
best = []
solve()
print(max(best))```

What I have tried:

Posted
Updated 17-Oct-18 10:54am
v4
Patrice T 17-Oct-18 15:27pm

What about learning Python to understand this code, then C to write the translation?

Rate this:

## Solution 2

C++ is quite expressive:
```#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

{
size_t n;
cin >> n;
vector<int> v(n);
for (auto & x : v)
cin >> x;
return v;
}
vector <int> solve(const vector<int> & inseq)
{
auto size = inseq.size();
vector<int> best(size);
for (size_t j=0; j<size; ++j)
{
vector<int> prev;
for (size_t k=0; k<j; ++k)
if ( inseq[j] % inseq[k] == 0)
prev.push_back(best[k]);
best[j] = prev.size() > 0 ? 1 + *max_element(prev.begin(), prev.end()) : 1;
}
return best;
}

int main()
{
auto best = solve(inseq);
cout << *max_element(best.begin(), best.end()) << endl;
}```
Rate this:

## Solution 1

Forget it. Python is too different from C to make any conversion simple. Many of the features of Python have no direct equivalent in C so you would need to design your own alternatives. Just use the design details of the program to write it in C.
CPallini 17-Oct-18 16:55pm

Maybe difficult to implement using `C`. On the other hand, `C++`...
Richard MacCutchan 18-Oct-18 4:02am

That's easy for you to say ...